package thread; public class TestSleep implements Runnable { private int time = 10; private Object lock = new Object(); public static void main(String[] args) { // TODO Auto-generated method stub TestSleep a = new TestSleep(); Thread t1 = new Thread(a); Thread t2 = new Thread(a); t1.start(); t2.start(); } @Override public void run() { // TODO Auto-generated method stub synchronized (lock) { for (int i = 0; i < 10; i++) { try { if (time == 5) lock.wait();//Thread.sleep(3000); //lock.notify(); time--; System.out.println(Thread.currentThread().getName() + "****" + time); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
运行结果
Thread-0****9
Thread-0****8
Thread-0****7
Thread-0****6
Thread-0****5
按理来说,线程1抢到锁,然后执行,到wait(),释放锁,应该CPU会运行其他线程,为什么运行到这里就停了(没有完全运行完,没有真正停止)
慕工程2701759
相关分类