object wait notify 实例一则

来源:互联网 发布:apache tomcat8.0下载 编辑:程序博客网 时间:2024/05/23 13:03

       其实,在Java中任何一个对象均有一个锁,而syhchronized同步块在调用时会检测该对象的锁定情况,主过程和子过程两个线程竞争同一对象时,在主过程中调用wait()方法,子过程即可进入synchronized同步块。而当子过程调用notify方法时,主过程又重新获得了对象锁,继续执行下去。实现如下:

package com.unclepeng.test;

//主过程
public class MainProgross2 {

 private static byte[] sign = new byte[0];
 
 public static void main(String[] args) {
  SubProgross subProgross = new SubProgross();
  Thread subThread = new Thread(subProgross);
  long currentTime = System.currentTimeMillis();
  System.out.println("execute main progross!");
  subThread.start();
  synchronized(sign){
   System.out.println("main progross get lock!");
   try {
    sign.wait();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("leave main progross!");
  long endTime = System.currentTimeMillis();
  System.out.println("total time: " + (endTime - currentTime));
 }
 
 //子过程
 static class SubProgross implements Runnable{
  public void run() {
   System.out.println("execute sub progross!");
   try {
    Thread.sleep(3000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   synchronized(sign){
    System.out.println("sub progross get lock!");
    sign.notify();
   }
   System.out.println("leave sub progross!");
  }
 
 }

}

其实在Java的Object类中notifyAll、notify、wait方法均为native方法,在windows中是通过外部的dll来实现其内部,可以说Object的notify及wait等操作是JVM级的,而第一种方法是程序级的,理论上来说,后一种要更高效。

0 0