Reprint Please specify source:http://blog.csdn.net/ns_code/article/details/17249321
Producer-consumer issues are a classic problem in threading models: producers and consumers share the same storage space during the same time period, producers produce data in space, and consumers take data.
Here are the production-consumption models for the following scenarios:
Producers are alternately producing two sets of data "name--1-and content--1", " name--2--> content--2", consumers are constantly alternately access to these two sets of data, where the " name--1" and " name--2" simulation is the name of the data, "content--1" and "Content--2" The content of the simulation for the data.
due to the uncertainty involved in threading the program, the following issues may occur:
1, assuming that the producer Line Cheng gang to the data storage space to add the name of the data, has not been added to the content of the information, the program switched to the consumer thread, the consumer thread will be the name of the information and the content of the previous information to link together;
2, the producer produces several times the data, the consumer only then starts to take the data, or is, after the consumer has completed once the data, has not waited for the producer to put in the new data, but also repeated took out the data which has been taken.
Problem 1 is obviously to be solved by synchronization, problem 2 requires inter-thread communication, the producer thread puts the data, notifies the consumer thread to take out the data, the consumer thread takes out the data, notifies the producer thread production data, which is implemented by the wait/notify mechanism.
The detailed implementation code is as follows:
[Java]View PlainCopy
- Class info{ //Defining information Classes
- private String name = "name"; Define the Name property, in order to distinguish it from the Name property of the following set
- private String content = "Content"; Defines the content property in order to distinguish it from the content property of the following set
- Private Boolean flag = true; //Set flag bit, first production at initial
- public synchronized Void set (String name,string content) {
- While (!flag) {
- try{
- super.wait ();
- }catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- }
- this.setname (name); //Set name
- try{
- Thread.Sleep (300);
- }catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- this.setcontent (content); //Set content
- Flag = false; //Change the flag to indicate that it can be taken away
- super.notify ();
- }
- public synchronized Void Get () {
- While (flag) {
- try{
- super.wait ();
- }catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- }
- try{
- Thread.Sleep (300);
- }catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- System.out.println (this.getname () +
- " --" + this.getcontent ());
- Flag = true; //Change the flag to indicate that it can be produced
- super.notify ();
- }
- public void SetName (String name) {
- this.name = name;
- }
- public void SetContent (String content) {
- this.content = content;
- }
- Public String GetName () {
- return this.name;
- }
- Public String getcontent () {
- return this.content;
- }
- }
- Class Producer implements runnable{ //multi-threaded via Runnable
- Private Info info = null; //Save Info Reference
- Public Producer (info info) {
- this.info = info;
- }
- public Void Run () {
- Boolean flag = true; //define marker bit
- For (int i=0;i<10;i++) {
- if (flag) {
- This.info.set ("name--1","content--1"); //Set name
- Flag = false;
- }else{
- This.info.set ("name--2","content--2"); //Set name
- 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<10;i++) {
- This.info.get ();
- }
- }
- }
- Public class threadcasedemo03{
- public static void Main (String args[]) {
- Info info = new info (); //Instantiate info object
- Producer Pro = new Producer (info); //producer
- Consumer con = new Consumer (info); //Consumer
- new Thread (PRO). Start ();
- //starts the producer thread and then starts the consumer thread
- try{
- Thread.Sleep (500);
- }catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- new Thread (Con). Start ();
- }
- }
The results of the implementation are as follows:
In addition, in the Run method, the number of cycles is the same, otherwise, when one side of the loop ends, the other side of the loop continues, it will block at the wait () method, and wait for the other party's notify notification.
Turn: Java concurrency Programming 13: Producer-consumer model (with code)