Sunday, 20 June 2021

Inter Thread Communication in java

multithreading replaces event loop programming by dividing your

tasks into discrete, logical units. Threads also provide a secondary benefit: they do away

with polling. Polling is usually implemented by a loop that is used to check some condition

repeatedly. Once the condition is true, appropriate action is taken. This wastes CPU time.

For example, consider the classic queuing problem, where one thread is producing some

data and another is consuming it. To make the problem more interesting, suppose that the

producer has to wait until the consumer is finished before it generates more data. In a polling system, the consumer would waste many CPU cycles while it waited for the producer to

produce. Once the producer was finished, it would start polling, wasting more CPU cycles

waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.

To avoid polling, Java includes an elegant interprocess communication mechanism via

the wait( ), notify( ), and notifyAll( ) methods. These methods are implemented as final

methods in Object, so all classes have them. All three methods can be called only from

within a synchronized context. Although conceptually advanced from a computer science

perspective, the rules for using these methods are actually quite simple:

• wait( ) tells the calling thread to give up the monitor and go to sleep until some

other thread enters the same monitor and calls notify( ).

• notify( ) wakes up a thread that called wait( ) on the same object.

• notifyAll( ) wakes up all the threads that called wait( ) on the same object. One of

the threads will be granted access.

These methods are declared within Object, as shown here:

final void wait( ) throws InterruptedException

final void notify( )

final void notifyAll( )

Additional forms of wait( ) exist that allow you to specify a period of time to wait.

Before working through an example that illustrates interthread communication, an

important point needs to be made. Although wait( ) normally waits until notify( ) or

notifyAll( ) is called, there is a possibility that in very rare cases the waiting thread could be

awakened due to a spurious wakeup. In this case, a waiting thread resumes without notify( )

or notifyAll( ) having been called. (In essence, the thread resumes for no apparent reason.)

Because of this remote possibility, Sun recommends that calls to wait( ) should take place

within a loop that checks the condition on which the thread is waiting. The following

example shows this technique.

 //Inter Thread Communication in java

class Shared

{

int n;

boolean available=false;

Shared(int x)

{

n=x;

}

synchronized void produce(int x)

{

while(available)

{

try

{

wait();

}

catch(InterruptedException e){}

}

n=x;

available=true;

notify();

}

synchronized int consume()

{

while(!available)

{

try

{

wait();

}

catch(InterruptedException e){}

}

available=false;

notify();

return n;

}

}

class Producer implements Runnable

{

Shared s;

Producer(Shared s1)

{

s=s1;

}

public void run()

{

for(int i=1;i<=10;i++)

{

s.produce(i);

System.out.println("Produced "+i);

}

}

}

class Consumer implements Runnable

{

Shared s;

Consumer(Shared s1)

{

s=s1;

}

public void run()

{

for(int i=1;i<=10;i++)

{

System.out.println("Consumed "+s.consume());

}

}

}

class InterThreadComDemo

{

public static void main(String args[])

{

Shared ob=new Shared(-1);

Producer p=new Producer(ob);

Consumer c=new Consumer(ob);

Thread t1=new Thread(p);

Thread t2=new Thread(c);

t1.start();

t2.start();

}

}

No comments:

Post a Comment