Synchronization and deadlock of threads

Source: Internet
Author: User
Tags ticket

If a thread pool is implemented through the Runnable interface, it means that the properties in the class will be shared by multiple threads, and the problem is the resource synchronization problem:

1. The delay of a thread causes other threads to operate on the resource share block either prematurely or later.

2. If multiple threads are in the process of determining the execution state of a problem, the thread becomes deadlocked.

The above problem can be resolved synchronously (note: Multiple threads need to synchronize when sharing the same resource, guaranteeing the integrity of the resource operation, but too much synchronization can cause deadlocks.)

Package Rubbableclass;/** Problems arising from delay functions in threads **/classMythread_eight implements runnable{Private intTicket =5; @Override Public voidrun () {//TODO Auto-generated method stubs         for(inti =0;i<Ten; i++){            if(ticket>0){                Try{Thread.Sleep (5); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } System. out. println ("Sell ticket: Ticket ="+ticket--); }        }    }    } Public classeigth { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsMythread_eight my =Newmythread_eight (); Thread T1=NewThread (My,"Thread A-"); Thread T2=NewThread (My,"Thread-B"); Thread T3=NewThread (My,"thread C -");        T1.start ();        T2.start ();    T3.start (); }}
View Code

Processing

1 Package Rubbableclass;2 /*3 * Synchronized operation is added.4  * */5 classMythread_nine implements runnable{6     Private intticket;7      PublicMythread_nine (intticket) {8         //TODO Auto-generated constructor stub9          This. Ticket =ticket;Ten     } One      A @Override -      Public voidrun () { -         //TODO Auto-generated method stubs the          for(inti =0; i<Ten; i++){ -Synchronized ( This) { -                 if(ticket>=0){ -                     Try { +Thread.Sleep (5); -}Catch(Exception e) { +                         //Todo:handle Exception A e.printstacktrace (); at}finally{ -System. out. println ("Sell ticket: Ticket ="+ticket--); -                     } -                 } -             } -         } in     } -      to } +  Public classNine { -  the      Public Static voidMain (string[] args) { *         //TODO Auto-generated method stubs $Mythread_nine my =NewMythread_nine (5);Panax NotoginsengThread T1 =NewThread (My,"Thread A-"); -Thread t2 =NewThread (My,"Thread-B"); theThread t3 =NewThread (My,"thread C -"); + T1.start (); A T2.start (); the T3.start (); +     } -  $}
View Code

Deadlock problem

1 Package Rubbableclass;2 /*3 * New Even objects, a call Zhang San called John Doe, through the Zhang San and John Doe of borrowing and painting problems, resulting in deadlock problems4 * Zhang San: Want to draw, give me the picture, give you the book5 * John Doe: If you want a book, give me the book.6  * 7 * If both parties are waiting for each other's response then it is easy to create a deadlock state8  * */9 classzhangsan{Ten      Public voidsay () { OneSystem. out. println ("Zhang San said to John Doe, want the book to give me the picture! "); A     } -      Public voidgetpicture () { -System. out. println ("got a picture."); the     } - } -  - classlisi{ +      Public voidsay () { -System. out. println ("John Doe to Zhang San said: Want to draw the book to me! "); +     } A      Public voidGetBook () { atSystem. out. println ("got the book!"); -     } - } -  Public classTen implements runnable{ -     Private StaticZhangsan ZS =NewZhangsan (); -     Private StaticLisi ls =NewLisi (); in     PrivateBoolean flag =false;//used to judge who spoke first . - @Override to      Public voidrun () { +         //TODO Auto-generated method stubs -         //Suppose Zhang Sanxian spoke. the         if(flag) { * synchronized (ZS) { $ Zs.say ();Panax Notoginseng                 Try { -Thread.Sleep ( -); the}Catch(Exception e) { +                     //Todo:handle Exception A e.printstacktrace (); the }synchronized (LS) { + zs.getpicture (); -                 } $             } $         } -         Else{ - synchronized (LS) { the Ls.say (); -                 Try {WuyiThread.Sleep ( -); the}Catch(Exception e) { -                     //Todo:handle Exception Wu e.printstacktrace (); - }synchronized (ZS) { About Ls.getbook (); $                 } -             } -         } -          A     } +      the      Public Static voidMain (string[] args) { -         //TODO Auto-generated method stubs $Ten My1 =Newten (); theTen My2 =Newten (); theMy1.flag =true; theMy2.flag =false; the  -  inThread T1 =NewThread (My1); theThread t2 =NewThread (My2); the T1.start (); About T2.start (); the      the      the      +     } -  the}
View Code

Synchronization and deadlock of threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.