Adv Java Program : Write a program in java which will show lifecycle (creation, sleep, and dead) of a thread. Program should print randomly the name of thread and value of sleep time. The name of the thread should be hard coded through constructor. The sleep time of a thread will be a random integer in the range 0 to 4999.

 Adv Java Program : Write a program in java which will show lifecycle (creation, sleep, and dead) of a thread. Program should print randomly the name of thread and value of sleep time. The name of the thread should be hard coded through constructor. The sleep time of a thread will be a random integer in the range 0 to 4999.



MyThread.java


Class MyThread extends Thread

{ public MyThread(String s)

{

super(s);

}

public void run()

{

System.out.println(getName()+"thread created.");

while(true)

{

System.out.println(this);

int s=(int)(math.random()*5000);

System.out.println(getName()+"is sleeping for :+s+"msec");

try{

Thread.sleep(s);

}

catch(Exception e)

{

}

}

}

Class ThreadLifeCycle

{

public static void main(String args[])

{

MyThread t1=new MyThread("shradha"),t2=new MyThread("pooja");

t1.start();

t2.start();

try

{

t1.join();

t2.join();

}

catch(Exception e)

{

}

System.out.println(t1.getName()+"thread dead.");

System.out.println(t2.getName()+"thread dead.");

}

}

Post a Comment

0 Comments