obj1.wait()
清晰写法
-
清晰写法(语义伪代码):
obj1.waitingThreadList.insert(currentThread);
-
清晰写法(语义伪代码,中文):
obj1.等待中线程列表.插入(当前线程);
-
通俗解释: 当前线程 进入 obj1的 等待中线程列表
-
大概意思: wait == “插入”
obj1.notify()
清晰写法
-
清晰写法(语义伪代码):
obj1.waitingThreadList.removeSomeOneThread();
-
清晰写法(语义伪代码,中文):
obj1.等待中线程列表.删除某一个线程();
-
通俗解释: obj1的 等待中线程列表 中 删除某一个线程
-
大概意思: notify == “删除”
obj1.notifyAll()
清晰写法
-
清晰写法(语义伪代码):
obj1.waitingThreadList.removeAllThread();
-
清晰写法(语义伪代码,中文):
obj1.等待中线程列表.删除全部线程();
-
通俗解释: obj1的 等待中线程列表 中 删除全部线程
-
大概意思: notifyAll == “删除全部”
完整代码
public class Example {
final static Object obj1 = new Object();
public static class Thread1 extends Thread{
@Override
public void run(){
synchronized (obj1){
try {
obj1.wait();//清晰写法: obj1.waitingThreadList.insert(currentThread);
//清晰写法(中文): obj1.等待中线程列表.插入(当前线程);
//大概意思: wait == "插入"
}catch (InterruptedException e){
}//end_of catch
}//end_of synchronized
}//end_of run
}//end_of class
public static class Thread2 extends Thread{
@Override
public void run(){
synchronized (obj1){
obj1.notify(); //清晰写法: obj1.waitingThreadList.removeSomeOneThread();
//清晰写法(中文): obj1.等待中线程列表.删除某一个线程();
//大概意思: notify == "删除"
obj1.notifyAll();//清晰写法: obj1.waitingThreadList.removeAllThread();
//清晰写法(中文): obj1.等待中线程列表.删除全部线程();
//大概意思: notifyAll == "删除全部"
}//end_of synchronized
}//end_of run
}//end_of class
public static void main(String[] args) {
Thread 线程1 = new Thread1();
Thread 线程2 = new Thread2();
线程1.start();
线程2.start();
}//end_of main
}//end_of class