Skip to content

Commit 0ac6ee2

Browse files
committed
Merge branch 'master' of github.com:sofkyle/CodeBase
2 parents 8e1bcf9 + dff4f77 commit 0ac6ee2

File tree

6 files changed

+257
-0
lines changed

6 files changed

+257
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package concurrency.cooperation.semaphore;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.Semaphore;
6+
import java.util.concurrent.locks.Condition;
7+
import java.util.concurrent.locks.ReentrantLock;
8+
9+
/**
10+
* @author: Kyle
11+
* @date: 2018/11/20 10:33
12+
*/
13+
public class ListPool {
14+
private int poolMaxSize = 3;
15+
16+
private int semaphorePermits = 5;
17+
18+
private List<String> list = new ArrayList<String>();
19+
20+
private Semaphore semaphore = new Semaphore(semaphorePermits);
21+
22+
private ReentrantLock lock = new ReentrantLock();
23+
24+
private Condition condition = lock.newCondition();
25+
26+
public ListPool() {
27+
for (int i = 0; i < poolMaxSize; i++) {
28+
list.add("string_" + (i + 1));
29+
}
30+
}
31+
32+
public String get() {
33+
String getString = null;
34+
try {
35+
semaphore.acquire();
36+
lock.lock();
37+
while (list.size() == 0) {
38+
condition.await();
39+
}
40+
getString = list.remove(0);
41+
lock.unlock();
42+
} catch (InterruptedException e) {
43+
e.printStackTrace();
44+
}
45+
46+
return getString;
47+
}
48+
49+
public void put(String value) {
50+
lock.lock();
51+
list.add(value);
52+
condition.signalAll();
53+
lock.unlock();
54+
semaphore.release();
55+
}
56+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package concurrency.cooperation.semaphore;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
/**
6+
* @author Kyle
7+
* @create 2018/11/20 09:51
8+
*/
9+
public class MultiEnterMultiDealMultiOutService {
10+
private Semaphore semaphore = new Semaphore(3);
11+
12+
public void sayHello() {
13+
try {
14+
semaphore.acquire();
15+
System.out.println("ThreadName=" + Thread.currentThread().getName() + "准备");
16+
System.out.println("Begin hello " + System.currentTimeMillis());
17+
for (int i = 0; i < 5; i++) {
18+
System.out.println(Thread.currentThread().getName() + "打印" + (i + 1));
19+
}
20+
System.out.println("End hello " + System.currentTimeMillis());
21+
semaphore.release();
22+
System.out.println("ThreadName=" + Thread.currentThread().getName() + "结束");
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package concurrency.cooperation.semaphore;
2+
3+
import java.util.concurrent.Semaphore;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* @author: Kyle
8+
* @date: 2018/11/20 10:24
9+
*/
10+
public class MultiEnterSingleDealMultiOutService {
11+
private Semaphore semaphore = new Semaphore(3);
12+
13+
private ReentrantLock lock = new ReentrantLock();
14+
15+
public void sayHello() {
16+
try {
17+
semaphore.acquire();
18+
System.out.println("ThreadName=" + Thread.currentThread().getName() + "准备");
19+
lock.lock();
20+
System.out.println("Begin hello " + System.currentTimeMillis());
21+
for (int i = 0; i < 5; i++) {
22+
System.out.println(Thread.currentThread().getName() + "打印" + (i + 1));
23+
}
24+
System.out.println("End hello " + System.currentTimeMillis());
25+
lock.unlock();
26+
semaphore.release();
27+
System.out.println("ThreadName=" + Thread.currentThread().getName() + "结束");
28+
} catch (InterruptedException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package concurrency.cooperation.semaphore;
2+
3+
/**
4+
* @author: Kyle
5+
* @date: 2018/11/20 10:03
6+
*/
7+
public class MyThread extends Thread {
8+
private MultiEnterMultiDealMultiOutService multiEnterMultiDealMultiOutService;
9+
10+
public MyThread(MultiEnterMultiDealMultiOutService multiEnterMultiDealMultiOutService) {
11+
this.multiEnterMultiDealMultiOutService = multiEnterMultiDealMultiOutService;
12+
}
13+
14+
@Override
15+
public void run() {
16+
multiEnterMultiDealMultiOutService.sayHello();
17+
}
18+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package concurrency.cooperation.semaphore;
2+
3+
import java.util.concurrent.Semaphore;
4+
import java.util.concurrent.locks.Condition;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
/**
8+
* @author: Kyle
9+
* @date: 2018/11/20 14:04
10+
*/
11+
public class ProducerAndConsumer {
12+
private volatile Semaphore producerSemaphore = new Semaphore(10);
13+
14+
private volatile Semaphore consumerSemaphore = new Semaphore(20);
15+
16+
private volatile ReentrantLock lock = new ReentrantLock();
17+
18+
private volatile Condition producerCondition = lock.newCondition();
19+
20+
private volatile Condition consumerCondition = lock.newCondition();
21+
22+
// Capacity: 4
23+
private volatile Object[] capacity = new Object[4];
24+
25+
private boolean isEmpty() {
26+
boolean isEmpty = true;
27+
for (int i = 0; i < capacity.length; i++) {
28+
if (capacity[i] != null) {
29+
isEmpty = false;
30+
break;
31+
}
32+
}
33+
34+
return isEmpty;
35+
}
36+
37+
private boolean isFull() {
38+
boolean isFull = true;
39+
for (int i = 0; i < capacity.length; i++) {
40+
if (capacity[i] == null) {
41+
isFull = false;
42+
break;
43+
}
44+
}
45+
46+
return isFull;
47+
}
48+
49+
private void produce() {
50+
try {
51+
producerSemaphore.acquire();
52+
lock.lock();
53+
while (isFull()) {
54+
producerCondition.await();
55+
}
56+
for (int i = 0; i < capacity.length; i++) {
57+
if (capacity[i] == null) {
58+
capacity[i] = "data";
59+
System.out.println(Thread.currentThread().getName() + " produce " + capacity[i]);
60+
break;
61+
}
62+
}
63+
consumerCondition.notifyAll();
64+
lock.unlock();
65+
} catch (InterruptedException e) {
66+
e.printStackTrace();
67+
} finally {
68+
producerSemaphore.release();
69+
}
70+
}
71+
72+
private void consume() {
73+
try {
74+
consumerSemaphore.acquire();
75+
lock.lock();
76+
while (isEmpty()) {
77+
consumerCondition.await();
78+
}
79+
for (int i = 0; i < capacity.length; i++) {
80+
if (capacity[i] != null) {
81+
System.out.println(Thread.currentThread().getName() + " consume " + capacity[i]);
82+
capacity[i] = null;
83+
break;
84+
}
85+
}
86+
consumerCondition.notifyAll();
87+
lock.unlock();
88+
} catch (InterruptedException e) {
89+
e.printStackTrace();
90+
} finally {
91+
consumerSemaphore.release();
92+
}
93+
94+
}
95+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package concurrency.cooperation.semaphore;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* @author: Kyle
9+
* @date: 2018/11/20 10:05
10+
*/
11+
public class SemaphoreTest {
12+
13+
@Test
14+
public void testMoreToOneService() {
15+
MultiEnterMultiDealMultiOutService multiEnterMultiDealMultiOutService = new MultiEnterMultiDealMultiOutService();
16+
MyThread[] threads = new MyThread[12];
17+
18+
for (int i = 0; i < threads.length; i++) {
19+
threads[i] = new MyThread(multiEnterMultiDealMultiOutService);
20+
threads[i].start();
21+
}
22+
23+
try {
24+
System.in.read();
25+
} catch (IOException e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)