Here's a simple program but reveal thread synchronization mechanism:
- only one thread can enter critical region at a fix time
- two thread enter critical region one by one, and loop
- the time won't affect thread synchronization
- two thread work together like pass ball
package com.demo.thread;
public class DemoThread {
public static void main(String[] args) {
Object obj = new Object();
Working worker1 = new Working("worker-1", obj, 2000);
Working worker2 = new Working("worker-2", obj, 20);
worker1.start();
worker2.start();
}
}
class Working extends Thread {
private String mName;
private Object mObj;
private long mTime;
public Working(String name, Object obj, long time) {
mName = name;
mObj = obj;
mTime = time;
}
public void run() {
while(true) {
try {
System.out.println(mName + " is waiting");
Thread.sleep(mTime);
System.out.println(mName + " is trying to enter critical region");
synchronized(mObj) {
System.out.println(mName + " has entered into critical region");
System.out.println(mName + " will notify other thread");
mObj.notify();
System.out.println(mName + " will hang");
mObj.wait();
System.out.println(mName + " will quit critical region");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
the output will like:
worker-1 is waiting
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
.....