将做工程过程比较重要的一些代码片段做个备份,如下的资料是关于java Thread的wait,notify,wait,sleep简单演示的代码,应该能对各朋友有一些好处。
package org.he.util;
public class Test extends Thread {
Object lock = null;
boolean notifyFlag = false;
public Test(Object lock, boolean notifyFlag) {
this.lock = lock;
this.notifyFlag = notifyFlag;
}
@Override
public void run() {
synchronized (lock) {
System.out.println((notifyFlag == true ? "(notifyThread)" : "") + Thread.currentThread().getName()
+ " hava in partA");
try {
if (notifyFlag)
else
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println((notifyFlag == true ? "(notifyThread)" : "") + Thread.currentThread().getName()
+ " hava in partB");
}
}
public static void main(String[] args) throws InterruptedException {
Object lock = new Object();
new Test(lock, false).start();
new Test(lock, false).start();
new Test(lock, false).start();
Thread.sleep(10000);
new Test(lock, true).start();
}
}