Inter-thread Communication:

It is discussed earlier, multi-threading replaces event loop programming by dividing our tasks into discrete and logical units. Threads also proved 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 the CPU cycles. For example, consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interacting, suppose that the producer has to wait until the consumer has 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.

To avoid polling, Java includes an elegant (smart) inter-process communication mechanism via the wait(), notify(), and notifyAll() methods. These methods are implemented as final methods in Object class, so all classes have them. All three methods can be called only from within a synchronized context. There are certain rules for using these methods.

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 one thread (normally first thread) that called wait on the same object.

notifyAll()

wakes up all the threads that called wait() on the same object. Normally the highest priority thread will run first.

These methods are declared within Object class, as shown here:

final void wait() throws InterruptedException
final void notify()

final void notifyAll()

Example

The way to write this program in Java is to use wait() and notify() to signal in both directions, as shown here:

		class Q
		{
		int n;
		boolean valueSet=false;
		synchronized int get ()
		{
		if(!valueSet)
		{
		try
		{
		wait();
		}
		catch (InterruptedException e)
		{
		System.out.println (e);
		}
		}
		System.out.println(“Got :” + n);
		valueSet = false;
		notify();
		return n;
		}
		synchronized void put (int n)
		{
		if(valueSet)
		{
		try
		{
		wait();
		}
		catch(InterruptedException e)
		{
		System.out.println(e);
		}
		}
		this.n=n;
		valueSet =true;
		System.out.println(“Put :” + n);
		notify ();
		}
		}
		class Producer implements Runnable
		{
		Q q1;
		Producer (Q q2)
		{
		q1 = q2;
		new Thread(this, “Producer”).start ();
		}
		public void run ( )
		{
		int i = 0;
		while (true)
		{
		q1.put (++i);
		}
		}
		}
		class Consumer implements Runnable
		{
		Q q1;
		Consumer (Q q2)
		{
		q1 =q2;
		new Thread (this, “consumer”).start ( );
		}
		public void run ( )
		{
		while (true)
		{
		q1.get ( );
		}
		}
		}
		class PC2
		{
		public static void main (String args [])
		{
		System.out.println(“Press Control-C to stop”);
		Q q1 = new Q( );
		new Producer(q1);
		new Consumer(q1);
		}
		}
	
Output

Press Control-C to stop

Put:1

Got:1

Put:2

Got:2

Put:3

Got:3

Put:4

Got:4

Put:5

Got:5

Deadlock

A special type of error that we need to avoid related specifically to multi-tasking is deadlock, which occurs when two threads have a circular dependency on a pair of synchronized objects. For example, suppose one thread enters the monitor on object X and another thread enters the monitor on object Y. If the thread in X tries to call any synchronized method on Y, it will block as expected. However, if the thread in Y, in turn tries to call any synchronized method on X, the thread waits forever, because to access X, it would have to release its own lock on Y so that the first thread could complete.

Deadlock is a difficult error to debug for two reasons:

  1. In general, it occurs only rarely when the two threads time-slice in just the right way.
  2. It may involve more than two threads and two synchronized objects.

Copyright 1999- Ducat Creative, All rights reserved.