



public class Test {public static void main(String[] args) {Object obj=new Object();Thread th1=new Thread(){@Overridepublic void run() {synchronized (obj){System.out.println("来三个包子!");try {obj.wait(); } catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("好嘞,吃包子!");}}};Thread th2=new Thread(){@Overridepublic void run() {synchronized (obj){System.out.println("好嘞,稍等五秒钟!");try {Thread.sleep(5000); } catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("包子做好啦!");obj.notify(); }}};th1.start();th2.start();}
}
- sleep(long m) ,在毫秒值结束后线程进入Runnable或者Blocked状态
- Wait(long m) ,在毫秒值结束之后如果还没有被唤醒,则自动唤醒进入Runnable或者Blocked状态
- notify() 随机唤醒单个线程
- notifyAll() 唤醒所有线程