Skip to content

Commit 4081e2b

Browse files
author
wb-hjk570755
committed
condition用例
1 parent 7e64a1a commit 4081e2b

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

src/com/blankj/concurrent/BoundedQueue.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
package com.blankj.concurrent;
2+
import java.util.LinkedList;
3+
import java.util.concurrent.locks.Condition;
4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
26

37
/**
48
* Description:
@@ -9,4 +13,57 @@
913
* @version 1.0 2020/9/22
1014
*/
1115
public class BoundedQueue {
16+
17+
private LinkedList<Object> buffer; //生产者容器
18+
private int maxSize ; //容器最大值是多少
19+
private Lock lock;
20+
private Condition fullCondition;
21+
private Condition notFullCondition;
22+
BoundedQueue(int maxSize){
23+
this.maxSize = maxSize;
24+
buffer = new LinkedList<Object>();
25+
lock = new ReentrantLock();
26+
fullCondition = lock.newCondition();
27+
notFullCondition = lock.newCondition();
28+
}
29+
30+
/**
31+
* 生产者
32+
* @param obj
33+
* @throws InterruptedException
34+
*/
35+
public void put(Object obj) throws InterruptedException {
36+
lock.lock(); //获取锁
37+
try {
38+
while (maxSize == buffer.size()){
39+
System.out.println("满了,添加的线程进入等待状态");
40+
notFullCondition.await(); //满了,添加的线程进入等待状态
41+
}
42+
buffer.add(obj);
43+
fullCondition.signal(); //通知
44+
} finally {
45+
lock.unlock();
46+
}
47+
}
48+
49+
/**
50+
* 消费者
51+
* @return
52+
* @throws InterruptedException
53+
*/
54+
public Object get() throws InterruptedException {
55+
Object obj;
56+
lock.lock();
57+
try {
58+
while (buffer.size() == 0){ //队列中没有数据了 线程进入等待状态
59+
System.out.println("队列中没有数据了 线程进入等待状态");
60+
fullCondition.await();
61+
}
62+
obj = buffer.poll();
63+
notFullCondition.signal(); //通知
64+
} finally {
65+
lock.unlock();
66+
}
67+
return obj;
68+
}
1269
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.blankj.concurrent;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
/**
7+
* Description:
8+
* Copyright: Copyright (c) 2012
9+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
10+
*
11+
* @author huangjk
12+
* @version 1.0 2020/9/22
13+
*/
14+
public class BoundedQueueTest {
15+
public static void main(String[] args) {
16+
BoundedQueue boundedQueue = new BoundedQueue(5);
17+
18+
ExecutorService executorService = Executors.newFixedThreadPool(2);
19+
executorService.execute(()->{
20+
for (int i=0;i<10000;i++){
21+
try {
22+
boundedQueue.put(new Object());
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
});
28+
executorService.execute(()->{
29+
for (int i=0;i<10000;i++){
30+
try {
31+
boundedQueue.get();
32+
} catch (InterruptedException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
});
37+
}
38+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.blankj.concurrent;
2+
3+
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.locks.Condition;
7+
import java.util.concurrent.locks.Lock;
8+
import java.util.concurrent.locks.ReentrantLock;
9+
/**
10+
* Description:
11+
* Copyright: Copyright (c) 2012
12+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
13+
*
14+
* @author huangjk
15+
* @version 1.0 2020/9/22
16+
*/
17+
public class ConditionUseCase {
18+
19+
public Lock lock = new ReentrantLock();
20+
public Condition condition = lock.newCondition();
21+
22+
public static void main(String[] args) {
23+
ConditionUseCase useCase = new ConditionUseCase();
24+
ExecutorService executorService = Executors.newFixedThreadPool (2);
25+
executorService.execute(new Runnable() {
26+
@Override
27+
public void run() {
28+
useCase.conditionWait();
29+
}
30+
});
31+
executorService.execute(new Runnable() {
32+
@Override
33+
public void run() {
34+
useCase.conditionSignal();
35+
}
36+
});
37+
}
38+
39+
public void conditionWait() {
40+
lock.lock();
41+
try {
42+
System.out.println(Thread.currentThread().getName() + "拿到锁了");
43+
System.out.println(Thread.currentThread().getName() + "等待信号");
44+
condition.await();
45+
System.out.println(Thread.currentThread().getName() + "拿到信号");
46+
}catch (Exception e){
47+
48+
}finally {
49+
lock.unlock();
50+
}
51+
}
52+
public void conditionSignal() {
53+
lock.lock();
54+
try {
55+
Thread.sleep(5000);
56+
System.out.println(Thread.currentThread().getName() + "拿到锁了");
57+
condition.signal();
58+
System.out.println(Thread.currentThread().getName() + "发出信号");
59+
}catch (Exception e){
60+
61+
}finally {
62+
lock.unlock();
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
 (0)