Currentthreaddemo - Java: Created by Prof. Priya.V, Site, Vitu 1 9/26/2012
Currentthreaddemo - Java: Created by Prof. Priya.V, Site, Vitu 1 9/26/2012
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
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 Threadsleep
{
public static void main(String args[])
{
Thread ct = Thread.currentThread();
System.out.println("\n The thread created is:"+ ct.getName());
System.out.println("\n The main thread priority value of "+ ct.getName() + "is = " +
ct.getPriority());
}
}
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);
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
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);
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");
}
}
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 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");
}
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