package com.cxz.currency.test;
public class Testee implements Runnable {
Object lock = null;
public boolean runFlag = false;
public Testee(Object lock) {
this.lock = lock;
}
public void run() {
synchronized (lock) {
try {
if (!runFlag) {
lock.wait();
}
System.out.println("Hello" + Thread.currentThread().getName());
} catch (InterruptedException ex) {
}
}
}
public void start() {
Thread thread = new Thread(this);
thread.start();
}
}
package com.cxz.currency.test;
public class Main {
public void test() throws InterruptedException{
Object lock = new Object();
Testee[] ts = new Testee[2];
ts[0] = new Testee(lock);
ts[1] = new Testee(lock);
ts[0].start();
ts[1].start();
Thread.sleep(100);
synchronized (lock) {
lock.notify();
}
}
public static void main(String[] args) throws InterruptedException{
new Main().test();
}
}
In the Main::test() I created 2 threads which were block at the lock.wait and after the main thread sleeped 100 milliseconds call the lock.notify and the lock.notifyAll you could see the differences.