Thread threads consumer and producer problem Code __java multithreading

Source: Internet
Author: User

Recently reviewed the thread, found running consumer and producer code parts, there will always be first consumption after production.

The source code is as follows:

public class Producerconsumer {public static void main (string[] args) {syncstack ss = new Syncsta
               CK ();
               Producer p = new Producer (ss);
               Consumer C = new Consumer (ss);
               New Thread (P). Start ();
               New Thread (P). Start ();

               New Thread (P). Start ();
               New Thread (c). Start ();
               New Thread (c). Start ();
        New Thread (c). Start ();
        } class Wotou {int id;
        Wotou (int id) {this.id = ID;
        Public String toString () {return "Wotou:" + ID;
        } class Syncstack {int index = 0;
       
        wotou[] ARRWT = new Wotou[6];
               Public synchronized void push (Wotou wt) {//is not available here, if interruptedexception occurs, it jumps out of the IF statement and continues down
                              The while will return the execution while (index = = arrwt.length) {try { This.wait()//The thread that locked the current object stops (if the current object is already locked)} catch (Interruptedexception e) {E
                       . Printstacktrace ();             
               } this.notifyall ();
               Arrwt[index] = WT;
        Index + +; The public synchronized Wotou pop () {//is not allowed to use if, if interruptedexception occurs, it jumps out of the IF statement and continues down// While it returns the execution while (index = = 0) {try {this.wait ();//Lock The thread holding the current object stops (if the current object is already locked)} catch (Interruptedexception e) {E.print
                       StackTrace ();
               } this.notifyall ();
               index--;
        return Arrwt[index];
        } class Producer implements Runnable {Syncstack SS = null;
        Producer (Syncstack ss) {THIS.SS = SS;
public void Run () {               for (int i=0; i<20; i++) {Wotou wt = new Wotou (i);
Ss.push (WT);
                       System.out.println ("produced:" + wt);
                       try {thread.sleep ((int) (Math.random () * 200));
                       catch (Interruptedexception e) {e.printstacktrace ();
        Class Consumer implements Runnable {Syncstack SS = null;
        Consumer (Syncstack ss) {THIS.SS = SS; public void Run () {for (int i=0; i<20; i++) {Wotou wt = Ss.pop
();
                       System.out.println ("Consumption:" + wt);
                       try {thread.sleep ((int) (Math.random () * 1000));
                       catch (Interruptedexception e) {e.printstacktrace ();                 }    
               }
        }
}
 

Run Result:
Consumption: wotou:0
Produced: wotou:0
Produced: Wotou:1
Produced: Wotou:2
Produced: Wotou:3
Produced: Wotou:4
Consumption: Wotou:4
Produced: Wotou:5
Produced: Wotou:6
Produced: Wotou:7
Consumption: Wotou:7
Produced: Wotou:8
Consumption: Wotou:8
Produced: Wotou:9
Consumption: Wotou:9
Produced: Wotou:10
Consumption: wotou:10
Produced: Wotou:11
Consumption: wotou:11
Produced: Wotou:12
Produced: Wotou:13
Consumption: Wotou:12
Produced: Wotou:14
Consumption: wotou:13
Consumption: wotou:14
Produced: wotou:15
Consumption: wotou:15
Produced: Wotou:16
Consumption: wotou:16
Produced: Wotou:17
Consumption: wotou:17
Produced: Wotou:18
Consumption: wotou:18
Produced: wotou:19
Consumption: wotou:19
Consumption: Wotou:6
Consumption: Wotou:5
Consumption: Wotou:3
Consumption: Wotou:2
Consumption: wotou:1

There should be a problem with the thread, and when the push () method is finished, to print the first few, another thread executes, leading to consumption before production.

Modify code: Put the print part in the push () and Pop () methods while the Pop () method returns the type void.

The code is as follows:

public class Producerconsumer {public static void main (string[] args) {syncstack ss = new Syncsta
               CK ();
               Producer p = new Producer (ss);
               Consumer C = new Consumer (ss);
 
               New Thread (P). Start ();
        New Thread (c). Start ();
        } class Wotou {int id;
        Wotou (int id) {this.id = ID;
        Public String toString () {return "Wotou:" + ID;
        } class Syncstack {int index = 0;
       
        wotou[] ARRWT = new Wotou[6];
                               Public synchronized void push (Wotou wt) {while (index = = arrwt.length) {try {
                       This.wait ();
                       catch (Interruptedexception e) {e.printstacktrace ();             
               } this.notifyall ();
                Arrwt[index] = WT; Index + +;
        System.out.println ("produced:" + wt);
                               public synchronized void Pop () {while (index = = 0) {try {
                       This.wait ();
                       catch (Interruptedexception e) {e.printstacktrace ();
               } this.notifyall ();
               index--;
        System.out.println ("Consumption:" + Arrwt[index]);
        } class Producer implements Runnable {Syncstack SS = null;
        Producer (Syncstack ss) {THIS.SS = SS; public void Run () {for (int i=0; i<20; i++) {Wotou wt = new Wo
                       Tou (i);
                      
                       Ss.push (WT);
                       try {thread.sleep ((int) (Math.random () * 200));
           catch (Interruptedexception e) {                    E.printstacktrace ();
        Class Consumer implements Runnable {Syncstack SS = null;
        Consumer (Syncstack ss) {THIS.SS = SS;
                      
                       public void Run () {for (int i=0; i<20; i++) {ss.pop ();
                       try {thread.sleep ((int) (Math.random () * 1000));
                       catch (Interruptedexception e) {e.printstacktrace ();
 }                     
               }
        }
}




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.