Skip to content

Commit 8fb577f

Browse files
committed
增加CountDownLatch实例
1 parent c9c7e2b commit 8fb577f

File tree

6 files changed

+106
-3
lines changed

6 files changed

+106
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.concurrency.CountDownLatch;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
/**
6+
*
7+
* @Project:javaConcurrency
8+
* @file:Conference.java
9+
*
10+
* @Author:chenssy
11+
12+
* @url : http://cmsblogs.com
13+
* @qq : 122448894
14+
*
15+
* @data:2015年9月6日
16+
*/
17+
public class Conference implements Runnable{
18+
private final CountDownLatch countDown;
19+
20+
public Conference(int count){
21+
countDown = new CountDownLatch(count);
22+
}
23+
24+
/**
25+
* 与会人员到达,调用arrive方法,到达一个CountDownLatch调用countDown方法,锁计数器-1
26+
* @author:chenssy
27+
* @data:2015年9月6日
28+
*
29+
* @param name
30+
*/
31+
public void arrive(String name){
32+
System.out.println(name + "到达.....");
33+
//调用countDown()锁计数器 - 1
34+
countDown.countDown();
35+
System.out.println("还有 " + countDown.getCount() + "没有到达...");
36+
}
37+
38+
@Override
39+
public void run() {
40+
System.out.println("准备开会,参加会议人员总数为:" + countDown.getCount());
41+
//调用await()等待所有的与会人员到达
42+
try {
43+
countDown.await();
44+
} catch (InterruptedException e) {
45+
}
46+
System.out.println("所有人员已经到达,会议开始.....");
47+
}
48+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.concurrency.CountDownLatch;
2+
3+
public class Participater implements Runnable{
4+
private String name;
5+
private Conference conference;
6+
7+
public Participater(String name,Conference conference){
8+
this.name = name;
9+
this.conference = conference;
10+
}
11+
12+
@Override
13+
public void run() {
14+
conference.arrive(name);
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.concurrency.CountDownLatch;
2+
3+
public class Test {
4+
public static void main(String[] args) {
5+
//启动会议室线程,等待与会人员参加会议
6+
Conference conference = new Conference(3);
7+
new Thread(conference).start();
8+
9+
for(int i = 0 ; i < 3 ; i++){
10+
Participater participater = new Participater("chenssy-0" + i , conference);
11+
Thread thread = new Thread(participater);
12+
thread.start();
13+
}
14+
}
15+
}

javaConcurrency/src/com/concurrency/Semaphore/Job.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package com.concurrency.Semaphore;
22

3+
/**
4+
*
5+
* @Project:javaConcurrency
6+
* @file:Job.java
7+
*
8+
* @Author:chenssy
9+
10+
* @url : http://cmsblogs.com
11+
* @qq : 122448894
12+
*
13+
* @data:2015Äê9ÔÂ6ÈÕ
14+
*/
315
public class Job implements Runnable{
416
private PrintQueue printQueue;
517

javaConcurrency/src/com/concurrency/Semaphore/PrintQueue.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
import java.util.concurrent.Semaphore;
44

5+
/**
6+
* 打印任务
7+
* @Project:javaConcurrency
8+
* @file:PrintQueue.java
9+
*
10+
* @Author:chenssy
11+
12+
* @url : http://cmsblogs.com
13+
* @qq : 122448894
14+
*
15+
* @data:2015年9月6日
16+
*/
517
public class PrintQueue {
618
private final Semaphore semaphore; //声明信号量
719

javaConcurrency/src/com/concurrency/Semaphore/Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class Test {
44
public static void main(String[] args) {
5-
Thread[] threads = new Thread[10];
5+
Thread[] threads = new Thread[5];
66

77
PrintQueue printQueue = new PrintQueue();
88

9-
for(int i = 0 ; i < 10 ; i++){
9+
for(int i = 0 ; i < 5 ; i++){
1010
threads[i] = new Thread(new Job(printQueue),"Thread_" + i);
1111
}
1212

13-
for(int i = 0 ; i < 10 ; i++){
13+
for(int i = 0 ; i < 5 ; i++){
1414
threads[i].start();
1515
}
1616
}

0 commit comments

Comments
 (0)