this is a test lalallalalalal
这是另外一条测试,啦啦啦啦啦啦
这是图片测试,啦啦啦啦(图片来自网络)
这是代码测试,咦,怎么添加代码呢?
1:
2: public class ProducerConsumer {
3: public static void main(String[] args) {
4: SyncStack ss = new SyncStack();
5: Producer p = new Producer(ss);
6: Consumer c = new Consumer(ss);
7: new Thread(p).start();
8: new Thread(p).start();
9: new Thread(p).start();
10: new Thread(c).start();
11: }
12: }
13:
14: class WoTou {
15: int id;
16: WoTou(int id) {
17: this.id = id;
18: }
19: public String toString() {
20: return "WoTou : " + id;
21: }
22: }
23:
24: class SyncStack {
25: int index = 0;
26: WoTou[] arrWT = new WoTou[6];
27:
28: public synchronized void push(WoTou wt) {
29: while(index == arrWT.length) {
30: try {
31: this.wait();
32: } catch (InterruptedException e) {
33: e.printStackTrace();
34: }
35: }
36: this.notifyAll();
37: arrWT[index] = wt;
38: index ++;
39: }
40:
41: public synchronized WoTou pop() {
42: while(index == 0) {
43: try {
44: this.wait();
45: } catch (InterruptedException e) {
46: e.printStackTrace();
47: }
48: }
49: this.notifyAll();
50: index--;
51: return arrWT[index];
52: }
53: }
54:
55: class Producer implements Runnable {
56: SyncStack ss = null;
57: Producer(SyncStack ss) {
58: this.ss = ss;
59: }
60:
61: public void run() {
62: for(int i=0; i<20; i++) {
63: WoTou wt = new WoTou(i);
64: ss.push(wt);
65: System.out.println("生产了:" + wt);
66: try {
67: Thread.sleep((int)(Math.random() * 200));
68: } catch (InterruptedException e) {
69: e.printStackTrace();
70: }
71: }
72: }
73: }
74:
75: class Consumer implements Runnable {
76: SyncStack ss = null;
77: Consumer(SyncStack ss) {
78: this.ss = ss;
79: }
80:
81: public void run() {
82: for(int i=0; i<20; i++) {
83: WoTou wt = ss.pop();
84: System.out.println("消费了: " + wt);
85: try {
86: Thread.sleep((int)(Math.random() * 1000));
87: } catch (InterruptedException e) {
88: e.printStackTrace();
89: }
90: }
91: }
92: }
:smile: 表情测试~
图片/代码/emoji