Here you can find out the nice example how to use wait and notify or notifyAll() – Niraj 2 days ago
If you are using notify() instead of notifyAll() it will triger only one thread in wait() state with high priority. If you are using notifyAll() it will triger all the thread which was in wait() state.
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
}
}