Sun River
          Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ...
          posts - 78,comments - 0,trackbacks - 0

          Java implements a very efficient interprocess communication which reduces the CPU’s idle time to a very great extent. It is been implemented through wait ( ), notify ( ) and notifyAll ( ) methods. Since these methods are implemented as final methods they are present in all the classes.

          The basic functionality of each one of them is as under:


          wait( ) acts as a intimation to the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).

          notify( ) is used as intimator to wake up the first thread that called wait( ) on the same object.

          notifyAll( ) as the term states wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first.


          public class WaitNotifyAllExample {
           
          public static void main(String[] args) {
           
          try {
          Object o = new Object();
          Thread thread1 = new Thread(new MyOwnRunnable("A", o));
          Thread thread2 = new Thread(new MyOwnRunnable("B", o));
          Thread thread3 = new Thread(new MyOwnRunnable("C", o));
           
          // synchronized keyword acquires lock on the object.
          synchronized (o) {
          thread1.start();
          // wait till the first thread completes execution.
          // thread should acquire the lock on the object
          // before calling wait method on it. Otherwise it will
          // throw java.lang.IllegalMonitorStateException 
          o.wait();
          thread2.start();
          // wait till the second thread completes execution
          o.wait();
          thread3.start();
          }
           
          }
          catch (InterruptedException e) {
          e.printStackTrace();
          }
           
          }
          }
           
          class MyOwnRunnable implements Runnable {
           
          private String threadName;
           
          private Object o;
           
          public MyOwnRunnable(String name, Object o) {
          threadName = name;
          this.o = o;
          }
           
          public void run() {
           
           
          synchronized (o) {
          for (int i = 0; i < 1000; i++) {
          System.out.println("Thread " + threadName + " Count : " + i);
          }
          // notify all threads waiting for the object o.
          // thread should acquire the lock on the object
          // before calling notify or notifyAll method on it. 
          // Otherwise it will throw java.lang.IllegalMonitorStateException 
          o.notifyAll();
          }
          }
          }
          posted on 2009-03-12 12:09 Sun River 閱讀(334) 評論(0)  編輯  收藏 所屬分類: Java SE
          主站蜘蛛池模板: 永春县| 河池市| 江川县| 临沧市| 弋阳县| 桂林市| 大悟县| 钟山县| 金堂县| 柘荣县| 广安市| 筠连县| 淄博市| 锦州市| 丹凤县| 弋阳县| 苗栗市| 乐山市| 馆陶县| 肃北| 新乡市| 山阳县| 新建县| 榕江县| 陵水| 朝阳县| 色达县| 简阳市| 米脂县| 乌拉特后旗| 集贤县| 喀喇| 南阳市| 林芝县| 桃园县| 阿克苏市| 韩城市| 米泉市| 仁布县| 宜川县| 四会市|