yield示例分析 public class TestYield { private static final Object lock = new Object(); public static void main(String[] args) { YieldThread t1 = new YieldThread(); YieldThread t2 = new YieldThread(); t1.start(); t2.start(); /** * t1得到cpu执行权后,执行run时,获取到l…
sleep让"当前线程"由“运行状态”进入到“休眠(阻塞)状态”,sleep结束,线程重新被唤醒时,它会由“阻塞状态”变成“就绪状态”,从而等待cpu的调度执行. 示例分析: public class TestSleep { private static final Object lock = new Object(); public static void main(String[] args) { SleepThread t1 = new SleepThread("t1&…
wait方法使"当前线程"进入阻塞(等待)状态. 示例分析: public class TestWait { public static void main(String[] args) throws InterruptedException { Thread t = new MyThread("t1"); synchronized (t){ //main线程持有t对象的锁 System.err.println(Thread.currentThread().getN…