Java学习-多线程交互

1-生产者消费者问题

1. 使用来存放数据
  1.1 把栈改造为支持线程安全
  1.2 把栈的边界操作进行处理,当栈里的数据是0的时候,访问pull的线程就会等待。 当栈里的数据是200的时候,访问push的线程就会等待
2. 提供一个生产者(Producer)线程类,生产随机大写字符压入到堆栈
3. 提供一个消费者(Consumer)线程类,从堆栈中弹出字符并打印到控制台
4. 提供一个测试类,使两个生产者和三个消费者线程同时运行

  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             System.out.printf("此时栈s的数据是:" + data + "  一共%d个\n", data.size());
 43         }
 44     }
 45 
 46     public static class Producer extends Thread { // 生产者线程类
 47         String name;
 48         stack s;
 49 
 50         public Producer(stack s, String name) {
 51             this.s = s;
 52             this.name = "Producer " + name;
 53         }
 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 { // 专门的测试类
 97         public void run() {
 98             stack s = new stack();
 99             for (int i = 0; i < 2; i++) { // 2个生产者
100                 Producer p = new Producer(s, String.valueOf(i));
101                 p.start();
102             }
103             for (int i = 0; i < 3; i++) { // 3个消费者
104                 Consumer c = new Consumer(s, String.valueOf(i));
105                 c.start();
106             }
107         }
108     }
109 
110     public static Character ranChar() { // 生成随机的大写字符
111         int s = (int) 'A';
112         int e = (int) '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 }

效果图:

猜你喜欢

转载自www.cnblogs.com/gilgamesh-hjb/p/12236390.html
今日推荐