1、wait(),notify(),notifyall():只能在锁下进行,synchronized(x),x.notify() / x.wait();
2、wait():放弃了锁,wait()作用是当前的线程
3、不同线程的同步
package com.j2se.test;
public class Test {
private static Object obj = new Object();
public static void main(String[] args) {
System.out.println("Hello,World --------------- start"
+ System.currentTimeMillis() / 1000);
InterruptThread thread = new InterruptThread();
thread.start();
synchronized (thread) {
try {
thread.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Hello,World --------------- end"
+ System.currentTimeMillis() / 1000);
}
private static class InterruptThread extends Thread {
public void run() {
try {
System.out.println("Thread");
sleep(5000);
synchronized (this) {
notifyAll();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
}