------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
THREADING IN JAVA
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
Q Create two Thread subclasses, one with a run( ) that starts up and then calls wait( ). The other class’ run( ) should capture the reference of the first Thread object. Its run( ) should call notifyAll( ) for the first thread after some number of seconds have passed, so the first thread can print a message.
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
NAME : SAHIN NISAR RAJ
ENROLL : 105200693009
COLLEGE : LAXMI INSTITUTE OF COMPUTER APPLICATIONS
ENROLL : 105200693009
COLLEGE : LAXMI INSTITUTE OF COMPUTER APPLICATIONS
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
package session8threads;class Thread1 implements Runnable
{
Thread thrd1;
Thread1()
{
thrd1 = new Thread(this,"Thread One");
thrd1.start();
}
public void run()
{
try
{
System.out.println("Thread First Started");
synchronized(this)
{
System.out.println("Thread First Entering into Wait State");
this.wait(20000);
System.out.println("Thread First Resumed after Notify");
}
} catch (Exception e)
{
System.out.println("Exception in First Thread" +e.getMessage());
}
}
}
class Thread2 implements Runnable
{
Thread thrd2;
Thread1 temp;
Thread2(Thread1 th)
{
thrd2 = new Thread(this,"Thread Two");
temp = th;
thrd2.start();
}
public void run()
{
try
{
System.out.println("Thread Two Started");
synchronized(temp)
{
System.out.println("Thread Two Notified Thread First");
temp.notify();
}
} catch (Exception e)
{
System.out.println("Exception in Second Thread" +e.getMessage());
}
}
}
public class Main
{
public static void main(String[] args)
{
Thread1 thrd1 = new Thread1();
Thread2 thrd2 = new Thread2(thrd1);
}
}
No comments:
Post a Comment