-
Java中生产者消费者模型的实现
普通类 -
- 支持
- 批判
- 提问
- 解释
- 补充
- 删除
-
-
使用wait()和notify()方法
Java Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72package cn.edu.bnu.lcell;
import java.util.LinkedList;
public class ProducerAndConsumer {
private static final int MAX_SIZE = 10;
private LinkedList<Integer> storage = new LinkedList<>();
public void begin() {
new Thread(new Producer()).start();
new Thread(new Consumer()).start();
}
class Producer implements Runnable {
@Override
public void run() {
while (true) {
synchronized (storage) {
try {
while (storage.size() == MAX_SIZE) {
System.out.println("Storage Full, Producer Wait...");
storage.wait();
}
if (storage.add(1)) {
System.out.println("Produce a Object, storage is " + storage.size());
storage.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
class Consumer implements Runnable {
@Override
public void run() {
while (true) {
synchronized (storage) {
try {
while (storage.size() == 0) {
System.out.println("Storage Empty, Consumer Wait...");
storage.wait();
}
storage.removeLast();
System.out.println("Consume a Object, storage is " + storage.size());
storage.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumer pac = new ProducerAndConsumer();
pac.begin();
}
}-
使用await()和signal()
Java Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerAndConsumer {
private static final int MAX_SIZE = 10;
private LinkedList<Integer> storage = new LinkedList<>();
private Lock lock = new ReentrantLock();
private Condition notFull = lock.newCondition();
private Condition notEmpty = lock.newCondition();
public void begin() {
new Thread(new Producer()).start();
new Thread(new Consumer()).start();
}
class Producer implements Runnable {
@Override
public void run() {
while (true) {
lock.lock();
try {
while (storage.size() == MAX_SIZE) {
System.out.println("Storage Full, Producer Wait...");
notFull.await();
}
if (storage.add(1)) {
System.out.println("Produce a Object, storage is " + storage.size());
notEmpty.signalAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
class Consumer implements Runnable {
@Override
public void run() {
while (true) {
lock.lock();
try {
while (storage.size() == 0) {
System.out.println("Storage Empty, Consumer Wait...");
notEmpty.await();
}
storage.removeLast();
System.out.println("Consume a Object, storage is " + storage.size());
notFull.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumer pac = new ProducerAndConsumer();
pac.begin();
}
}-
使用LinkedBlockingQueue
Java Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55import java.util.concurrent.LinkedBlockingQueue;
public class ProducerAndConsumer {
private static final int MAX_SIZE = 10;
private LinkedBlockingQueue<Integer> storage = new LinkedBlockingQueue<>(MAX_SIZE);
public void begin() {
new Thread(new Producer()).start();
new Thread(new Consumer()).start();
}
class Producer implements Runnable {
@Override
public void run() {
while (true) {
try {
storage.put(1);
System.out.println("Produce a Object, storage is " + storage.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
class Consumer implements Runnable {
@Override
public void run() {
while (true) {
try {
storage.take();
System.out.println("Consume a Object, storage is " + storage.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumer pac = new ProducerAndConsumer();
pac.begin();
}
} -
-
- 标签:
- 模型
- start
- newthread
- java
- 消费者
- e.printstacktrace
- overridepublicvoidrun
- true
- 实现
- try
- catch
- system.out.println
- 生产者
- storageis+storage.size
- interruptedexceptione
-
加入的知识群:
学习元评论 (0条)
聪明如你,不妨在这 发表你的看法与心得 ~