wait notify (一)

来源:百度文库 编辑:神马文学网 时间:2024/06/13 00:39:42
wait()、notify()、notifyAll()是三个定义在Object类里的方法,可以用来控制线程的状态。
这三个方法最终调用的都是jvm级的native方法。随着jvm运行平台的不同可能有些许差异。
如果对象调用了wait方法就会使持有该对象的线程把该对象的控制权交出去,然后处于等待状态。 如果对象调用了notify方法就会通知某个正在等待这个对象的控制权的线程可以继续运行。 如果对象调用了notifyAll方法就会通知所有等待这个对象控制权的线程继续运行。
其中wait方法有三个over load方法:
wait()
wait(long)
wait(long,int)
wait方法通过参数可以指定等待的时长。如果没有指定参数,默认一直等待直到被通知。
以下是一个演示代码,以最简洁的方式说明复杂的问题:
简要说明下:
NotifyThread是用来模拟3秒钟后通知其他等待状态的线程的线程类;
WaitThread是用来模拟等待的线程类;
等待的中间对象是flag,一个String对象;
main方法中同时启动一个Notify线程和三个wait线程;
Java代码',1)">
public class NotifyTest {       private  String flag = "true";          class NotifyThread extends Thread{           public NotifyThread(String name) {               super(name);           }           public void run() {                    try {                   sleep(3000);//推迟3秒钟通知               } catch (InterruptedException e) {                   e.printStackTrace();               }                                  flag = "false";                   flag.notify();           }       };          class WaitThread extends Thread {           public WaitThread(String name) {               super(name);           }              public void run() {                                  while (flag!="false") {                       System.out.println(getName() + " begin waiting!");                       long waitTime = System.currentTimeMillis();                       try {                           flag.wait();                       } catch (InterruptedException e) {                           e.printStackTrace();                       }                       waitTime = System.currentTimeMillis() - waitTime;                       System.out.println("wait time :"+waitTime);                   }                   System.out.println(getName() + " end waiting!");                          }       }          public static void main(String[] args) throws InterruptedException {           System.out.println("Main Thread Run!");           NotifyTest test = new NotifyTest();           NotifyThread notifyThread =test.new NotifyThread("notify01");           WaitThread waitThread01 = test.new WaitThread("waiter01");           WaitThread waitThread02 = test.new WaitThread("waiter02");           WaitThread waitThread03 = test.new WaitThread("waiter03");           notifyThread.start();           waitThread01.start();           waitThread02.start();           waitThread03.start();       }      }
public class NotifyTest {private String flag = "true";class NotifyThread extends Thread{public NotifyThread(String name) {super(name);}public void run() {try {sleep(3000);//推迟3秒钟通知} catch (InterruptedException e) {e.printStackTrace();}flag = "false";flag.notify();}};class WaitThread extends Thread {public WaitThread(String name) {super(name);}public void run() {while (flag!="false") {System.out.println(getName() + " begin waiting!");long waitTime = System.currentTimeMillis();try {flag.wait();} catch (InterruptedException e) {e.printStackTrace();}waitTime = System.currentTimeMillis() - waitTime;System.out.println("wait time :"+waitTime);}System.out.println(getName() + " end waiting!");}}public static void main(String[] args) throws InterruptedException {System.out.println("Main Thread Run!");NotifyTest test = new NotifyTest();NotifyThread notifyThread =test.new NotifyThread("notify01");WaitThread waitThread01 = test.new WaitThread("waiter01");WaitThread waitThread02 = test.new WaitThread("waiter02");WaitThread waitThread03 = test.new WaitThread("waiter03");notifyThread.start();waitThread01.start();waitThread02.start();waitThread03.start();}}