Java Multithreading (synchronization and deadlock issues, producer and consumer issues)

Source: Internet
Author: User

First we look at the synchronization and deadlock problem:


The so-called deadlock is that a owns Banana,b owns Apple.

A to B said: "If you give me Apple, I will give you the banana."

B to a said: "You give me banana, I will give Apple to you."


But both A and B are waiting for each other's response, so the end result is that a does not get apple,b and banana. This cycle of death is a deadlock.

So we can simulate the description above and write the following code:


Class A represents a person,

public class A {public void Say () {System.out.println ("A said to B:if you give me the apple, I'll give you the banana." );} public void Get () {System.out.println ("A get the apple.");}

Class B represents the person B,

public class B {public void Say () {System.out.println ("B said to A:If you give me the banana, I'll give you the apple." );} public void Get () {System.out.println ("B get the Banana.");}}

Class Threaddeadlock represents the deadlock class,

public class Threaddeadlock implements Runnable{private static a a=new a ();p rivate static B b=new B ();p ublic boolean flag= false;public void Run () {if (flag) {synchronized (a) {A.say (); Try{thread.sleep (500);} catch (Interruptedexception e) {e.printstacktrace ();} Synchronized (b) {a.get ();}}} Else{synchronized (b) {B.say (); Try{thread.sleep (500);} catch (Interruptedexception e) {e.printstacktrace ();} Synchronized (a) {B.get ();}}}}

Here is the main class:

public class Main{public static void Main (string[] args) {threaddeadlock t1=new threaddeadlock (); Threaddeadlock t2=new Threaddeadlock (); t1.flag=true;t2.flag=false; Thread tha=new thread (t1); Thread thb=new thread (t2); Tha.start (); Thb.start ();}}


Program Run Result:

A said to B:if you give me the apple, I'll give you the banana.
B said to A:If you give me the banana, I'll give you the apple.


From the above program, we can find that two threads are waiting for each other's execution to complete, so that the program will not continue to execute, resulting in a deadlock operation phenomenon.


Here we look at producer and consumer issues:


The so-called producer and consumer problem, is very simple, the process is the producer constantly produce products, consumers continue to take away products.

Producer produces Product,consumer consumer product.

So, we define the product class first:

public class Product {private String name= "Product";p rivate boolean flag=false;public String getName () {return name;} public void SetName (String name) {this.name = name;} Public synchronized void Set (String name) {if (!flag) {try{super.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} This.setname (name); Try{thread.sleep (300);} catch (Interruptedexception e) {e.printstacktrace ();} Flag=false;super.notify ();} Public synchronized void Get () {if (flag) {try{super.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} Try{thread.sleep (300);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (This.getname ()); Flag=true;super.notify ();}}

Here joins the wait and wake, and adds a flag bit flag,flag to true when the expression can be produced, but cannot be taken away, at this time if the thread executes to the consumer thread, then should wait, if flag is false, it means can be taken away, but not production, if the producer thread runs, You should wait.


Producer Class:

public class Producer implements Runnable{private product Product=null;public Producer (product product) {this.product= Product;} public void Run () {for (int i=0;i<50;++i) {this.product.set ("Product");}}}

Consumer class:

public class Consumer implements Runnable{private product Product=null;public Consumer (product product) {this.product= Product;} public void Run () {for (int i=0;i<50;++i) {try{thread.sleep (100);} catch (Interruptedexception e) {e.printstacktrace ();} This.product.get ();}}}

Then the main class:

public class Main{public static void Main (string[] args) {Product product=new product (); Producer pro=new Producer (product); Consumer con=new Consumer (product); new Thread (PRO). Start (); new Thread (Con). Start ();}}

So we know that every production of producers of a product, consumers take away a product, consumers take a product each, it is necessary to wait for the production of producers.

Java Multithreading (synchronization and deadlock issues, producer and consumer issues)

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.