0% found this document useful (0 votes)
31 views

Currentthreaddemo - Java: Created by Prof. Priya.V, Site, Vitu 1 9/26/2012

The document contains code for demonstrating threading concepts in Java like creating threads, setting thread priority, thread synchronization, thread communication using wait and notify. The CurrentThreadDemo code shows getting the current thread and changing its name. Threadcreate codes demonstrate extending Thread class and implementing Runnable interface to create threads. Threadsleep, Threadjoin codes show using sleep and join methods. Threadsynchro codes demonstrate synchronized keyword for thread synchronization. Threadcomm codes demonstrate communication between threads using wait and notify methods.

Uploaded by

sai vivek p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Currentthreaddemo - Java: Created by Prof. Priya.V, Site, Vitu 1 9/26/2012

The document contains code for demonstrating threading concepts in Java like creating threads, setting thread priority, thread synchronization, thread communication using wait and notify. The CurrentThreadDemo code shows getting the current thread and changing its name. Threadcreate codes demonstrate extending Thread class and implementing Runnable interface to create threads. Threadsleep, Threadjoin codes show using sleep and join methods. Threadsynchro codes demonstrate synchronized keyword for thread synchronization. Threadcomm codes demonstrate communication between threads using wait and notify methods.

Uploaded by

sai vivek p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CurrentThreadDemo.

java
class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
// change the name of the thread
t.setName("My Thread");
System.out.println("After name change: " + t);
try {
for(int n = 5; n > 0; n--) {
System.out.println(n);
Thread.sleep(1000);
}
}catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
}
}

Threadcreate1.java
class Sumthread extends Thread
{
int i,sum=0;
Thread th;
Sumthread()
{
th = new Thread(this, "My thread");
System.out.println("\n The thread created is:"+ th.getName());
th.start();
}
public void run()
{
for(i=1;i<=5;i++)
{
sum +=i;
System.out.println("\n Sum of numbers from 1 up to "+i+"="+sum);
}
}

Created by
Prof. Priya.V, SITE,VITU Page 1 9/26/2012
}
class Threadcreate1
{
public static void main(String args[])
{
new Sumthread();
}
}

Threadcreate2.java
class Sumthread extends Thread
{

int i,sum=0;
public void run()
{
for(i=1;i<=5;i++)
{
sum +=i;
System.out.println("\n Sum of numbers from 1 up to "+i+"="+sum);
}
}
}
class Threadcreate2
{
public static void main(String args[])
{
Sumthread st = new Sumthread();
Thread th = new Thread(st, "My thread2");
System.out.println("\n The thread created is:"+ th.getName());
th.start();
}
}

Threadcreate3.java

class Sumthread implements Runnable


{
int i,sum=0;

Created by
Prof. Priya.V, SITE,VITU Page2 9/26/2012
Thread th;
Sumthread()
{
th = new Thread(this, "My thread3");
System.out.println("\n The thread created is:"+ th.getName());
System.out.println("\n Before starting the thread \n Is the thread alive ? :"+th.isAlive());
th.start();
System.out.println("\n After starting the thread \n Is the thread alive ? :"+th.isAlive());
}
public void run()
{
for(i=1;i<=5;i++)
{
sum +=i;
System.out.println("\n Sum of numbers from 1 up to "+i+"="+sum);
}
}
}
class Threadcreate3
{
public static void main(String args[])
{
new Sumthread();
}
}

Threadcreate4.java
class Sumthread implements Runnable
{
int i,sum=0;
public void run()
{
for(i=1;i<=5;i++)
{
sum +=i;
System.out.println("\n Sum of numbers from 1 up to "+i+"="+sum);
}}
}
class Threadcreate4
{
public static void main(String args[])

Created by
Prof. Priya.V, SITE,VITU Page 3 9/26/2012
{
Sumthread st = new Sumthread();
Thread th = new Thread(st, "My thread4");
System.out.println("\n The thread created is:"+ th.getName());
System.out.println("\n Before starting the thread \n Is the thread alive ? :"+th.isAlive());
th.start();
System.out.println("\n After starting the thread \n Is the thread alive ? :"+th.isAlive());
}}

Threadsleep.java

class Sumthread implements Runnable


{
int i,sum = 0;
public void run()
{
for(i=1;i<=5;i++)
{
sum +=i;
try
{
if(i%2==0)
Thread.sleep(25);
}
catch(InterruptedException e)
{
;
}

System.out.println("\n Sum of numbers from 1 up to "+i+"="+sum);


}
}
}
class Factthread implements Runnable
{
int i,n,fact=1;
public void run()
{
for(i=1;i<=5;i++)
{
fact *= i;
try
{
Created by
Prof. Priya.V, SITE,VITU Page 4 9/26/2012
if(i%3==0)
Thread.sleep(25);
}
catch(InterruptedException e)
{
;
}

System.out.println("\n Factorial of"+i+"="+ fact);


}
}
}

class Threadsleep
{
public static void main(String args[])
{
Thread ct = Thread.currentThread();
System.out.println("\n The thread created is:"+ ct.getName());

Sumthread st = new Sumthread();


Factthread ft = new Factthread();
Thread sumt = new Thread(st, "Sum thread");
Thread factt = new Thread(ft, "Factorial thread");

System.out.println("\n The main thread priority value of "+ ct.getName() + "is = " +
ct.getPriority());

System.out.println("\n The priority value of "+ sumt.getName() + "is = " +


sumt.getPriority());

System.out.println("\n The priority value of "+ factt.getName() + "is = " +


factt.getPriority());
sumt.start();
factt.start();

}
}

Created by
Prof. Priya.V, SITE,VITU Page 5 9/26/2012
Threadjoin.java
class Sumthread implements Runnable

{
int i,sum = 0;
public void run()
{
for(i=1;i<=5;i++)
{
sum +=i;
System.out.println("\n Sum of numbers from 1 up to "+i+"="+sum);
}
}
}
class Factthread implements Runnable
{
int i,n,fact=1;
public void run()
{
for(i=1;i<=5;i++)
{
fact *= i;
System.out.println("\n Factorial of"+i+"="+ fact);
}
}
}
class Threadjoin
{
public static void main(String args[])
{
Sumthread st = new Sumthread();
Factthread ft = new Factthread();
Thread sumt = new Thread(st, "Sum thread");
Thread factt = new Thread(ft, "Factorial thread");
sumt.setPriority(Thread.NORM_PRIORITY -2);
factt.setPriority(Thread.NORM_PRIORITY +2);

System.out.println("\n The thread created is:"+ sumt.getName());


System.out.println("\n The thread created is:"+ factt.getName());
System.out.println("\n The priority value of "+ sumt.getName() + "is = " +
sumt.getPriority());

Created by
Prof. Priya.V, SITE,VITU Page 6 9/26/2012
sumt.start();
System.out.println("\n The priority value of "+ factt.getName() + "is = " +
factt.getPriority());
try
{
sumt.join();
}
catch(InterruptedException e)
{
;
}

factt.start();
System.out.println("\n The thread created is"+ factt.getName());
}
}

Threadpriority.java

class Sumthread implements Runnable


{
int i,sum = 0;
public void run()
{
for(i=1;i<=5;i++)
{
sum +=i;
System.out.println("\n Sum of numbers from 1 up to "+i+"="+sum);
if (i==4)
Thread.yield();
}
}
}

Created by
Prof. Priya.V, SITE,VITU Page 7 9/26/2012
class Factthread implements Runnable
{
int i,n,fact=1;
public void run()
{
for(i=1;i<=5;i++)
{
fact *= i;
System.out.println("\n Factorial of"+i+"="+ fact);
}
}
}
class Threadpriority
{
public static void main(String args[])
{
Sumthread st = new Sumthread();
Factthread ft = new Factthread();
Thread sumt = new Thread(st, "Sum thread");
Thread factt = new Thread(ft, "Factorial thread");
sumt.setPriority(Thread.NORM_PRIORITY -2);
factt.setPriority(Thread.NORM_PRIORITY +2);

System.out.println("\n The thread created is:"+ sumt.getName());


System.out.println("\n The thread created is:"+ factt.getName());
System.out.println("\n The priority value of "+ sumt.getName() + "is = " +
sumt.getPriority());
System.out.println("\n The priority value of "+ factt.getName() + "is = " +
factt.getPriority());
sumt.start();
factt.start();
}
}

Created by
Prof. Priya.V, SITE,VITU Page8 9/26/2012
Threadsynchro1.java
class Printing
{
synchronized void printnumber(int n)
{
System.out.println("Start");
for(int j=n;j>0;j--)
{
try
{
if(j==n/2)
Thread.sleep(1000);
}
catch(InterruptedException e)
{
;
}
System.out.print(" " +j);
}
System.out.println("End");
}
}
class Threadserve implements Runnable
{
int n;
Printing pt;
Thread th;
Threadserve(Printing p, int x)
{
n=x;
pt=p;
th = new Thread(this);
th.start();
}
public void run()
{
pt.printnumber(n);
}
}

Created by
Prof. Priya.V, SITE,VITU Page 9 9/26/2012
class Threadsynchro1
{
public static void main(String args[])
{
Printing p = new Printing();
Threadserve ts1 = new Threadserve(p,16);
Threadserve ts2 = new Threadserve(p,8);
Threadserve ts3 = new Threadserve(p,10);
}
}

Threadsynchro2.java

class Printing
{
void printnumber(int n)
{
System.out.println("Start");
for(int j=n;j>0;j--)
{
try
{
if(j==n/2)
Thread.sleep(100);
}
catch(InterruptedException e)
{
;
}
System.out.print(" " +j);
}
System.out.println("End");
}
}

class Threadserve implements Runnable


{
int n;
Printing pt;
Thread th;

Created by
Prof. Priya.V, SITE,VITU Page 10 9/26/2012
Threadserve(Printing p, int x)
{
n=x;
pt=p;
th = new Thread(this);
th.start();
}
public void run()
{
synchronized(pt){
pt.printnumber(n);
}
}
}
class Threadsynchro2
{
public static void main(String args[])
{
Printing p = new Printing();
Threadserve ts1 = new Threadserve(p,16);
Threadserve ts2 = new Threadserve(p,8);
Threadserve ts3 = new Threadserve(p,10);
}
}

Created by
Prof. Priya.V, SITE,VITU Page 11 9/26/2012
Threadcomm.java (without wait and notify)

import java.io.*;
class Message
{
String mesg;
void Readmesg()
{
try
{
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ins);
System.out.println("Type in a message:");
mesg = br.readLine();
}

catch(IOException e)
{
System.out.println("IO Error");
}
}

void Printmesg()
{
System.out.println("The received message is:" + mesg);
}
}

class Receive implements Runnable


{
Message ms;
Thread th;
Receive(Message ms)
{
this.ms= ms;
th = new Thread(this);
th.start();
}
public void run()
{
ms.Readmesg();
}
}
Created by
Prof. Priya.V, SITE,VITU Page 12 9/26/2012
class Print implements Runnable
{
Message ms;
Thread th;
Print(Message ms)
{
this.ms=ms;
th=new Thread(this);
th.start();
}
public void run()
{
ms.Printmesg();
}
}

class Threadcomm
{
public static void main(String args[])
{
Message mymsg = new Message();
for(int i=0;i<3;i++)
{
new Print(mymsg);
new Receive(mymsg);
}
}
}

Created by
Prof. Priya.V, SITE,VITU Page 13 9/26/2012
Threadcomm2.java (with wait and notify)

import java.io.*;
class Message
{
String mesg;
boolean received = false;
synchronized void Readmesg()
{
try
{
while(received)
wait();
}
catch(InterruptedException e)
{
System.out.println("Thread interrupted while waiting");
}

try
{
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ins);
System.out.println("Type in a message:");
mesg = br.readLine();
}

catch(IOException e)
{
System.out.println("IO Error");
}

received=true;
notify();

}
synchronized void Printmesg()
{
try
{
while(!received)
wait();
}

Created by
Prof. Priya.V, SITE,VITU Page 14 9/26/2012
catch(InterruptedException e)
{
System.out.println("Thread interrupted while waiting");
}

System.out.println("The received message is:" + mesg);


received = false;
notify();
}
}

class Receive implements Runnable


{
Message ms;
Thread th;
Receive(Message ms)
{
this.ms= ms;
th = new Thread(this);
th.start();
}
public void run()
{
ms.Readmesg();
}
}
class Print implements Runnable
{
Message ms;
Thread th;
Print(Message ms)
{
this.ms=ms;
th=new Thread(this);
th.start();
}
public void run()
{
ms.Printmesg();
}
}

Created by
Prof. Priya.V, SITE,VITU Page 15 9/26/2012
class Threadcomm2
{
public static void main(String args[])
{
Message mymsg = new Message();
for(int i=0;i<3;i++)
{
new Print(mymsg);
new Receive(mymsg);
}
}
}

Created by
Prof. Priya.V, SITE,VITU Page 16 9/26/2012

You might also like