在这里您可以找到如何使用 wait 和 notify 或 notifyAll() 的好例子 – Niraj 2 天前
如果您使用的是 notify() 而不是 notifyAll() 它将仅触发一个处于 wait() 状态的具有高优先级的线程。如果您使用的是 notifyAll() ,它将触发所有处于 wait() 状态的线程。
package com.construction.house;
import com.construction.labours.LabourCement;
import com.construction.labours.LabourPainting;
import com.construction.labours.LabourPolishMarbel;
public class House implements Runnable{
public House() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
House myHouse = new House();
LabourCement labourCement = new LabourCement(myHouse,"Cement");
labourCement.start();
LabourPainting labourPaining = new LabourPainting(myHouse,"Painter");
labourPaining.start();
LabourPolishMarbel labourPolish = new LabourPolishMarbel(myHouse,"PolishMan");
labourPolish.start();
}
boolean isPolished = false,isCemented = false,isPaited = false;
public synchronized void workAsDemand() throws InterruptedException {
if (!isPolished) {
isPolished = true;
System.out.println(Thread.currentThread().getName()+"--->>Polish in progress");
wait();
System.out.println(Thread.currentThread().getName()+"--->>Polish Completed");
}
else if (!isPaited) {
System.out.println(Thread.currentThread().getName()+"--->>Painting house in Progress");
isPaited = true;
//notify();
wait();
System.out.println(Thread.currentThread().getName()+"--->>Painting house in Completed");
}
else if (!isCemented) {
System.out.println(Thread.currentThread().getName()+"---->>Cemented house");
isCemented = true;
notifyAll();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
workAsDemand();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.construction.labours;
public class LabourCement extends Thread {
public LabourCement() {
// TODO Auto-generated constructor stub
}
public LabourCement(Runnable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public LabourCement(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public LabourCement(ThreadGroup arg0, Runnable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public LabourCement(ThreadGroup arg0, String arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public LabourCement(Runnable arg0, String arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public LabourCement(ThreadGroup arg0, Runnable arg1, String arg2) {
super(arg0, arg1, arg2);
// TODO Auto-generated constructor stub
}
public LabourCement(ThreadGroup arg0, Runnable arg1, String arg2, long arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}
}
package com.construction.labours;
public class LabourPolishMarbel extends Thread {
public LabourPolishMarbel() {
// TODO Auto-generated constructor stub
}
public LabourPolishMarbel(Runnable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public LabourPolishMarbel(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public LabourPolishMarbel(ThreadGroup arg0, Runnable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public LabourPolishMarbel(ThreadGroup arg0, String arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public LabourPolishMarbel(Runnable arg0, String arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public LabourPolishMarbel(ThreadGroup arg0, Runnable arg1, String arg2) {
super(arg0, arg1, arg2);
// TODO Auto-generated constructor stub
}
public LabourPolishMarbel(ThreadGroup arg0, Runnable arg1, String arg2, long arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}
}
package com.construction.labours;
public class LabourPainting extends Thread {
public LabourPainting() {
// TODO Auto-generated constructor stub
}
public LabourPainting(Runnable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public LabourPainting(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public LabourPainting(ThreadGroup arg0, Runnable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public LabourPainting(ThreadGroup arg0, String arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public LabourPainting(Runnable arg0, String arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public LabourPainting(ThreadGroup arg0, Runnable arg1, String arg2) {
super(arg0, arg1, arg2);
// TODO Auto-generated constructor stub
}
public LabourPainting(ThreadGroup arg0, Runnable arg1, String arg2, long arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}
}