Getting Started with Java-sync and deadlock

Source: Internet
Author: User
Tags ticket

Getting Started with Java-sync and deadlock Synchronous

To resolve the synchronization of resource sharing, you can do this by using synchronous code blocks and synchronization methods.

 

1  PackageSep19;2 3 classMyThreadImplementsrunnable{4     Private intTicket=5;5      Public voidRun () {//overwrite the Run method6          for(inti=0;i<100;i++){7             if(ticket>0){8                 Try{9Thread.Sleep (3000);Ten}Catch(interruptedexception e) { One e.printstacktrace (); A                 } -SYSTEM.OUT.PRINTLN ("Sell ticket: ticket=" +ticket--); -             } the         } -     } - } -  Public classsyncdemo01{ +      Public Static voidMain (string[] args) { -MyThread mt=NewMyThread (); +Thread t1=NewThread (MT); AThread t2=NewThread (MT); atThread t3=NewThread (MT); - T1.start (); - T2.start (); - T3.start (); -     } -}
Sell ticket: ticket=5 sellticket: Ticket =3 sellticket: Ticket =4 Sell ticket: ticket=2 Sell ticket: Ticket =1 Sell ticket: Ticket= 0

Synchronizing code blocks

Add the keyword before the code that needs to be synchronized:synchronized (synchronization object) {The code that needs to be synchronized;}

Synchronization method:

You can use the SYNCHRONIZED keyword to declare a synchronization method.

dead Lock

Too many synchronizations can also create deadlocks, and two of threads are waiting for each other to finish, so that the thread will not continue to execute down, causing a deadlock.

The classic case of threading--producers and consumers
Package Sep19;class info{private string name= "Li Xinghua";p rivate string content= "Java instructor";p ublic void SetName (string name) { THIS.name = name;} public void SetContent (String content) {this.content = content;} Public String GetName () {return name;} Public String getcontent () {return content;}} Class Producer implements Runnable{private info info=null;public Producer (info info) {this.info=info;} public void Run () {boolean flag=false;for (int i = 0; i <; i++) {if (flag) {this.info.setName ("Li Xinghua"); try {Thread.Sleep (100);} catch (Interruptedexception e) {e.printstacktrace ();} This.info.setContent ("Java lecturer"); flag=false;} Else{this.info.setname ("mldn"); try {thread.sleep);} catch (Interruptedexception e) {e.printstacktrace ();} This.info.setContent ("www.mldn.java.com"); flag=true;}}} Class Consumer implements Runnable{private info info=null;public Consumer (info info) {this.info=info;} public void Run () {for (int i = 0; i <; i++) {try {Thread.Sleep]} catch (Interruptedexception e) {e.printstackt RacE ();} System.out.println (This.info.getName () + "------->" +this.info.getcontent ());}} public class ThreadCaseDemo01 {public static void main (string[] args) {info i=new info (); Producer p=new Producer (i); Consumer con=new Consumer (i); new Thread (P). Start (); new Thread (Con). Start ();}}

Li Xinghua------->www.mldn.java.com Li Xinghua------->www.mldn.java.commldn------->java lecturer mldn-------> Java lecturer Li Xinghua------->www.mldn.java.com Li Xinghua------->java lecturer mldn------->www.mldn.java.commldn-------> Java lecturer mldn------->www.mldn.java.commldn------->java lecturer mldn------->www.mldn.java.commldn-------> Java lecturer Li Xinghua------->www.mldn.java.com Li Xinghua------->java lecturer Li Xinghua------->www.mldn.java.commldn-------> Java lecturer mldn------->www.mldn.java.com Li Xinghua------->www.mldn.java.com Li Xinghua------->www.mldn.java.com Li Xinghua------- >java lecturer Li Xinghua------->www.mldn.java.commldn------->java lecturer Li Xinghua------->www.mldn.java.commldn-------> Java lecturer Li Xinghua------->www.mldn.java.commldn------->java lecturer mldn------->www.mldn.java.com Li Xinghua-------> Java lecturer mldn------->www.mldn.java.commldn------->java lecturer Li Xinghua------->www.mldn.java.commldn-------> Java lecturer mldn------->www.mldn.java.commldn------->java lecturer Li Xinghua------->www.mldn.java.commldn-------> Java lecturer mldn------->www.mldn.java.com Li Xinghua------->java SpeakingTeacher mldn------->www.mldn.java.com Li Xinghua------->java lecturer Li Xinghua------->www.mldn.java.commldn-------> Java lecturer Li Xinghua------->www.mldn.java.commldn------->java lecturer mldn------->www.mldn.java.commldn-------> Java lecturer mldn------->www.mldn.java.commldn------->java lecturer Li Xinghua------->www.mldn.java.com Li Xinghua------->java lecturer

This code occurs duplicate production and name content mismatch phenomenon, need to join the synchronization, that is, the SetName and setcontent set to the same block of code to complete, solve its dislocation problem. The problem of solving dislocation is solved by using the waiting-wake mechanism in the Obeject class.

Package Sep19;class info{private string name= "Li Xinghua";p rivate string content= "Java Instructor";p Rivate boolean flag=false; public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String getcontent () {return content;} public void SetContent (String content) {this.content = content;} Public synchronized void Set (String name,string content) {if (!flag) {try {super.wait ();} catch (Interruptedexception e) {E . Printstacktrace ();}} This.setname (name); try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} This.setcontent (content); Flag=false;super.notify ();} Public synchronized void get () {try {Thread.Sleep],} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (This.getname () + "----->" +this.getcontent ()); Flag=false;super.notify ();}} Class Producer implements Runnable{private info info=null;public Producer (info info) {this.info=info;} public void Run () {boolean flag=false;for (int i = 0; i <; i++) {if (flag) {This.info.set ("Li Xinghua"," Java lecturer "); flag=false;} Else{this.info.set ("Mldn", "www.mldn.java.com"); flag=true;}}} Class Consumer implements Runnable{private info info=null;public Consumer (info info) {this.info=info;} public void Run () {for (int i = 0; i <; i++) {try {Thread.Sleep]} catch (Interruptedexception e) {e.printstackt Race ();} This.info.get ();}}} public class ThreadCaseDemo01 {public static void main (string[] args) {info i=new info (); Producer p=new Producer (i); Consumer con=new Consumer (i); new Thread (P). Start (); new Thread (Con). Start ();}}

Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua-----> Java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua-----> Java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua-----> Java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua-----> Java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua-----> Java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua-----> Java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua-----> Java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer MLDN-----&GT;WWW.MLDN.JAVA.COMMLDN-----> www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java lecturer MLDN-----> www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java lecturer MLDN-----> www.mldn.java.com Li Xinghua-----&GT Java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua-----> Java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer mldn----->www.mldn.java.com Li Xinghua----->java Lecturer

  

Getting Started with Java-sync and deadlock

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.