Description about wait() notify() notifyAll() stop() suspend() and resume(),interprocess comminucation in java to avoid polling..

The use of the implicit monitors in Java objects is powerful, but you can achieve a more subtle level of control through inter-process communication. As you will see, this is especially easy in Java.

  • Multithreading replaces event loop programming by dividing your tasks into discrete and 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 inteprocess 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 method. 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 the first thread that called wait( ) on the same object.
  • notifyAll( ) wakes up all the threads that called wait( ) on the same object. The 
    highest priority thread will run first.
These methods are declared within Object, as shown here:
final void wait( ) throws InterruptedException 
final void notify( ) 
final void notifyAll( )


  • Core Java provides a complete control over multithreaded program. You can develop a multithreaded program which can be suspended, resumed or stopped completely based on your requirements. There are various static methods which you can use on thread objects to control their behavior. Following table lists down those methods:

SNMethods with Description
1public void suspend()
This method puts a thread in suspended state and can be resumed using resume() method.
2public void stop()
This method stops a thread completely.
3public void resume()
This method resumes a thread which was suspended using suspend() method.
4public void wait()
Causes the current thread to wait until another thread invokes the notify().
5public void notify()
Wakes up a single thread that is waiting on this object's monitor.
Be aware that latest versions of Java has deprecated the usage of suspend( ), resume( ), and stop( ) methods and so you need to use available alternatives.


class MyThread implements Runnable {
  Thread thrd;
  boolean suspended;
  boolean stopped;

  MyThread(String name) {
    thrd = new Thread(this, name);
    suspended = false;
    stopped = false;
    thrd.start();
  }

  public void run() {
    try {
      for (int i = 1; i < 10; i++) {
        System.out.print(".");
        Thread.sleep(50);
        synchronized (this) {
          while (suspended)
            wait();
          if (stopped)
            break;
        }
      }
    } catch (InterruptedException exc) {
      System.out.println(thrd.getName() + " interrupted.");
    }
    System.out.println("\n" + thrd.getName() + " exiting.");
  }

  synchronized void stop() {
    stopped = true;
    suspended = false;
    notify();
  }

  synchronized void suspend() {
    suspended = true;
  }

  synchronized void resume() {
    suspended = false;
    notify();
  }
}

public class Main {
  public static void main(String args[]) throws Exception {
    MyThread mt = new MyThread("MyThread");
    Thread.sleep(100);
    mt.suspend();
    Thread.sleep(100);

    mt.resume();
    Thread.sleep(100);

    mt.suspend();
    Thread.sleep(100);

    mt.resume();
    Thread.sleep(100);

    mt.stop();
  } 
} 

Comments

Post a Comment

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another