One, multi-threaded:
Ticket Window Simple example:
Public classSaleticket { Public Static classSale implements runnable{Private intticket=Ten;//@Override//Public void Run () {//While (true) {//synchronized (this) {//Synchronous code block synchronized (this) locks the object//if (ticket>0) {//try {//Thread.Sleep (+);//} catch (Interruptedexception e) {// //TODO auto-generated Catch block//e.printstacktrace ();// }//System.out.println (Thread.CurrentThread (). GetName () + "sold out one, ticket remaining:" +ticket--);//}else break;// }// }// // }@Override Public voidrun () { for(intI=0;i< -; i++) {tell (); } } PublicSynchronizedvoidTell () {//Synchronization Method if(ticket>0){ Try{Thread.Sleep ( -); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System. out. println (Thread.CurrentThread (). GetName () +"sell one, ticket remaining:"+ticket--); } } } Public Static voidMain (string[] args) {Sale s=NewSale (); Thread T1=NewThread (S,"Conductor 1"); Thread T2=NewThread (S,"Conductor 2"); Thread T3=NewThread (S,"Conductor 3"); T1.start (); T2.start (); T3.start (); }}
Application results:
Conductor 1 sold out, ticket remaining: 10
Conductor 1 sold out, ticket remaining: 9
Conductor 1 sold out, ticket remaining: 8
Conductor 1 sold out, ticket remaining: 7
Conductor 3 sold out, ticket remaining: 6
Conductor 1 sold out, ticket remaining: 5
Conductor 2 sold out, ticket remaining: 4
Conductor 2 sold out, ticket remaining: 3
Conductor 3 sold out, ticket remaining: 2
Conductor 3 sold out, ticket remaining: 1
Two, thread deadlock:
Synchronization of threads can cause deadlocks, and deadlocks occur when two of threads hold each other's waiting items (actually, something that is shared by two threads). Deadlocks can occur as long as there are two threads and two objects
/** * * * a simple deadlock class * When the object of the deadlock class Flag==1 (TD1), first locks O1, sleeps 500 milliseconds * while TD1 while sleeping another Flag==0 object (TD2) thread starts, Locks O2 first, sleeps 500 milliseconds * TD1 Sleep After the end of the need to lock O2 to continue execution, and at this time O2 has been td2 locked; * TD2 After the end of sleep need to lock O1 to continue execution, while O1 is TD1 locked; * TD1, TD2 wait for each other, need to get the other locked resources to continue execution, thereby deadlock. */ Public classDeadLock implements runnable{ Public intFlag =1; //static objects are shared by all objects of the class Private StaticObject O1 =NewObject (), O2 =NewObject (); @Override Public voidrun () {System. out. println ("flag="+flag); if(Flag = =1) {synchronized (O1) {Try{Thread.Sleep ( -); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } synchronized (O2) {System. out. println ("1"); } } } if(Flag = =0) {synchronized (O2) {Try{Thread.Sleep ( -); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } synchronized (O1) {System. out. println ("0"); } } } } Public Static voidMain (string[] args) {DeadLock TD1=NewDeadLock (); DeadLock TD2=NewDeadLock (); Td1.flag=1; Td2.flag=0; NewThread (TD1,"TD1"). Start (); NewThread (TD2,"TD2"). Start (); } }
Operation Result:
Flag=1
Flag=0
resulting in a deadlock.
Java Multithreading and deadlocks