Java_Thread producer and consumer Demo, java_threaddemo

Source: Internet
Author: User

Java_Thread producer and consumer Demo, java_threaddemo

1 package com. bjsxt. thread. demo; 2 public class ProducerConsumer {3/** 4 * producer and consumer 5 * @ param args 6 */7 public static void main (String [] args) {// simulate thread 8 SyncStack ss = new SyncStack (); 9 Producer p = new Producer (ss); 10 Consumer c = new Consumer (ss ); 11 new Thread (p ). start (); // enable Thread 12 new Thread (c ). start (); // enable thread 13} 14} 15 16/** 17 * Woto class 18 */19 class WoTo {20 int id; 21 WoTo (int id) {22 this. id = id; 23} 24 public String toString () {25 return "WoTo:" + id; 26} 27} 28 29/** 30 * box (used to hold steamed buns) 31 * @ author wenfei 32 */33 class SyncStack {34 int index = 0; 35 WoTo [] arrwt = new WoTo [6]; 36 37 public synchronized void push (WoTo wt) {38 39 while (index = arrwt. length) {40 41 try {42 this. wait (); // tentative current object 43 44} catch (InterruptedException e) {45 e. printStackTrace (); 46} 47} 48 this. Y (); // wake up the current thread 49 arrwt [index] = wt; 50 index ++; 51} 52 53 public synchronized WoTo pop () {54 while (index = 0) {55 56 try {57 this. wait (); 58} catch (InterruptedException e) {59 e. printStackTrace (); 60} 61} 62 this. Y (); 63 index --; 64 return arrwt [index]; 65} 66} 67 68/** 69 * Producer 70*71 * @ author wenfei 72 */73 class Producer implements Runnable {74 SyncStack ss = null; 75 76 Producer (SyncStack ss) {77 this. ss = ss; 78} 79 80 @ Override 81 public void run () {82 // produce wt 83 for (int I = 0; I <= 100; I ++) {84 WoTo wt = new WoTo (I); 85 ss. push (wt); // enter the 86 System in the basket. out. println ("produced --->" + wt); 87 try {88 // Thread. sleep (1000); // each time a sleep is produced 89 threads per second. sleep (int) Math. random () * 1000); 90} catch (InterruptedException e) {91 // TODO Auto-generated catch block 92 e. printStackTrace (); 93} 94} 95} 96 97} 98 99/** 100 * Consumer 101*102 * @ author wenfei103 */104 class Consumer implements Runnable {105 SyncStack ss = null; 106 107 Consumer (SyncStack ss) {108 this. ss = ss; 109} 110 111 @ Override112 public void run () {113 for (int I = 0; I <= 100; I ++) {114 WoTo wt = ss. pop (); 115 System. out. println ("consumed --->" + wt); 116 try {117 // Thread. sleep (1000); // 118 Thread per second of sleep consumption. sleep (int) Math. random () * 1000); // 119} catch (InterruptedException e) {120 // TODO Auto-generated catch block121 e. printStackTrace (); 122} 123 // System. out. println (wt); 124} 125} 126 127}

 

 

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.