public class TextDead implements Runnable{public static void main(String[] args){ TextDead td1=new TextDead(); TextDead td2=new TextDead(); td1.flag=0; td2.flag=1; Thread t1=new Thread(td1); Thread t2=new Thread(td2); t1.start(); t2.start(); }int flag = 0;@Overridepublic void run() {if(flag==0){ try { TextDead.a(); TextDead.b(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }}if(flag==1){ try { TextDead.b(); TextDead.a(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}public static synchronized void a() throws InterruptedException{ Thread.sleep(10); System.out.println("SDFDSA");}public static synchronized void b() throws InterruptedException{ Thread.sleep(10); System.out.println(123);}}
我自己寫了一個線程死鎖的程序,為什么鎖不住
qq_之恩赫奧丶_0
2016-06-12 09:02:21