日期:2014-05-20 浏览次数:20851 次
package com.tdt.test; public class NotifyTest { public static final String LOCK="LOCK_MAIN"; public static void main(String[] args) { for(int i=0;i<=10;i++){ new Work("线程"+i).start(); } for(int i=0;i<=10;i++){ synchronized (LOCK) { LOCK.notify(); try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } } } class Work extends Thread{ public Work(String threadName) { super(threadName); } @Override public void run() { synchronized (NotifyTest.LOCK) { try { NotifyTest.LOCK.wait(); } catch (Exception e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+"得到通知........."); } }
try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); }
@Override public void run() { synchronized (NotifyTest.LOCK) { try { System.out.println("****"); //这加一句看看效果。 NotifyTest.LOCK.wait(); } catch (Exception e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+"得到通知........."); }