关于线程死锁
public class Test11 extends Thread { public static void main(String[] args) { Test11 t = new Test11(); t.start();//t线程开始执行 try { Thread.sleep(500);//让主线程等待0.5秒,此时t线程应该执行结束 } catch (InterruptedException e) { e.printStackTrace(); } synchronized (t) { try { System.out.println("wait for t to complete..."); t.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("total:" + t.total); } } int total; public void run() { synchronized (this) {//运行期间保持锁 for (int i = 0; i < 100; i++) { total += i; } System.out.println(total); notify();//唤醒等待此对象锁的一个线程 } }} synchronized (t) { try { if(t.total==0) //加仪判断,如果条件成立,说明就等待,否则不等待了。 { System.out.println("wait for t to complete..."); t.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("total:" + t.total); }