1- producers and consumers
1. Use a stack to store data
1.1 stack to support the transformation of thread-safe
to process 1.2 stack border operation, when the stack data is 0, the access pull the thread will wait. When the stack when the data is 200, access push thread will wait
2. Provide a producer (Producer) thread class, production random uppercase characters pushed onto the stack
3. Provide a consumer (Consumer) thread class, from stack pop up a character and printed to the console
4. provide a test class, the two producers and consumers three threads to run simultaneously
1 package multiplethread; 2 3 import java.util.LinkedList; 4 import java.util.List; 5 6 public class Test_Producer_Consumer { 7 public static class stack { 8 List<Character> data = new LinkedList<>(); 9 10 public synchronized void push(Character c) { 11 if (data.size() < 200) { 12 data.add(c); 13 this.notify(); 14 } else { 15 try { 16 this.wait(); 17 } catch (InterruptedException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } 21 } 22 } 23 24 public synchronized Character pull() { 25 if (data.size() > 0) { 26 Character c = data.remove(data.size() - 1); 27 this.notify(); 28 return c; 29 } else { 30 try { 31 this.wait(); 32 } catch (InterruptedException e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } 36 return null; 37 } 38 39 } 40 41 public synchronized void Print () { 42 is System.out.printf ( "s data stack at this time is:" + data + "total of the% d \ n-" , data.size ()); 43 is } 44 is } 45 46 is public static class producer the extends the thread { // Manufacturer thread class 47 String name; 48 Stack S; 49 50 public producer (Stack S, String name) { 51 is the this .s = S; 52 is the this .name = "producer" + name; 53 is } 54 55 public void run() { 56 while (true) { 57 Character c = ranChar(); 58 s.push(c); 59 System.out.println(this.name + " 压入:" + c); 60 s.print(); 61 try { 62 this.sleep(100); 63 } catch (InterruptedException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } 67 } 68 } 69 70 } 71 72 public static class Consumer extends Thread { // 消费者线程类 73 String name; 74 stack s; 75 76 public Consumer(stack s, String name) { 77 this.s = s; 78 this.name = "Consumer " + name; 79 } 80 81 public void run() { 82 while (true) { 83 Character c = s.pull(); 84 System.out.println(this.name + " 弹出:" + c); 85 s.print(); 86 try { 87 this.sleep(100); 88 } catch (InterruptedException e) { 89 // TODO Auto-generated catch block 90 e.printStackTrace(); 91 } 92 } 93 } 94 } 95 96 public static class TestThread { // special test class 97 public void RUN () { 98 Stack S = new new Stack (); 99 for ( int I = 0; I <2; I ++) { // 2 production by 100 Producer P = new new Producer (S, String.valueOf (I)); 101 p.start (); 102 } 103 for ( int I = 0; I <. 3; I ++) { // . 3 th consumer 104 C = Consumer new new Consumer (S, String.valueOf (I)); 105 c.start (); 106 } 107 } 108 } 109 110 public static Character ranChar () { // generate random uppercase characters 111 int S = ( int ) 'A' ; 112 int E = ( int ) 'the Z' ; 113 int n-= E - S +. 1 ; 114 int RND = ( int ) (Math.floor (Math.random () * n-) + S) ; 115 return (char) rnd; 116 } 117 118 public static void main(String[] args) { 119 TestThread t = new TestThread(); 120 t.run(); 121 122 } 123 }
Renderings: