在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法。
以下是本文目录大纲:
一.CountDownLatch用法
二.CyclicBarrier用法
三.Semaphore用法
一.CountDownLatch用法
CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能。比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了。
CountDownLatch类只提供了一个构造器:
1
|
public CountDownLatch( int count) { };
|
然后下面这3个方法是CountDownLatch类中最重要的方法:
|
public void await() throws InterruptedException { };
public boolean await( long timeout, TimeUnit unit) throws InterruptedException { };
public void countDown() { };
|
下面看一个例子大家就清楚CountDownLatch的用法了:
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
|
public class Test {
public static void main(String[] args) {
final CountDownLatch latch = new CountDownLatch( 2 );
new Thread(){
public void run() {
try {
System.out.println( "子线程" +Thread.currentThread().getName()+ "正在执行" );
Thread.sleep( 3000 );
System.out.println( "子线程" +Thread.currentThread().getName()+ "执行完毕" );
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
new Thread(){
public void run() {
try {
System.out.println( "子线程" +Thread.currentThread().getName()+ "正在执行" );
Thread.sleep( 3000 );
System.out.println( "子线程" +Thread.currentThread().getName()+ "执行完毕" );
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
try {
System.out.println( "等待2个子线程执行完毕..." );
latch.await();
System.out.println( "2个子线程已经执行完毕" );
System.out.println( "继续执行主线程" );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
|
执行结果:
|
线程Thread- 0 正在执行
线程Thread- 1 正在执行
等待 2 个子线程执行完毕...
线程Thread- 0 执行完毕
线程Thread- 1 执行完毕
2 个子线程已经执行完毕
继续执行主线程
|
二.CyclicBarrier用法
字面意思回环栅栏,通过它可以实现让一组线程等待至某个状态之后再全部同时执行。叫做回环是因为当所有等待线程都被释放以后,CyclicBarrier可以被重用。我们暂且把这个状态就叫做barrier,当调用await()方法之后,线程就处于barrier了。
CyclicBarrier类位于java.util.concurrent包下,CyclicBarrier提供2个构造器:
|
public CyclicBarrier( int parties, Runnable barrierAction) {
}
public CyclicBarrier( int parties) {
}
|
参数parties指让多少个线程或者任务等待至barrier状态;参数barrierAction为当这些线程都达到barrier状态时会执行的内容。
然后CyclicBarrier中最重要的方法就是await方法,它有2个重载版本:
|
public int await() throws InterruptedException, BrokenBarrierException { };
public int await( long timeout, TimeUnit unit) throws InterruptedException,BrokenBarrierException,TimeoutException { };
|
第一个版本比较常用,用来挂起当前线程,直至所有线程都到达barrier状态再同时执行后续任务;
第二个版本是让这些线程等待至一定的时间,如果还有线程没有到达barrier状态就直接让到达barrier的线程执行后续任务。
下面举几个例子就明白了:
假若有若干个线程都要进行写数据操作,并且只有所有线程都完成写数据操作之后,这些线程才能继续做后面的事情,此时就可以利用CyclicBarrier了:
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
|
public class Test {
public static void main(String[] args) {
int N = 4 ;
CyclicBarrier barrier = new CyclicBarrier(N);
for ( int i= 0 ;i<N;i++)
new Writer(barrier).start();
}
static class Writer extends Thread{
private CyclicBarrier cyclicBarrier;
public Writer(CyclicBarrier cyclicBarrier) {
this .cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
System.out.println( "线程" +Thread.currentThread().getName()+ "正在写入数据..." );
try {
Thread.sleep( 5000 );
System.out.println( "线程" +Thread.currentThread().getName()+ "写入数据完毕,等待其他线程写入完毕" );
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e){
e.printStackTrace();
}
System.out.println( "所有线程写入完毕,继续处理其他任务..." );
}
}
}
|
执行结果:
1
2
3
4
5
6
7
8
9
10
11
12
|
线程Thread- 0 正在写入数据...
线程Thread- 3 正在写入数据...
线程Thread- 2 正在写入数据...
线程Thread- 1 正在写入数据...
线程Thread- 2 写入数据完毕,等待其他线程写入完毕
线程Thread- 0 写入数据完毕,等待其他线程写入完毕
线程Thread- 3 写入数据完毕,等待其他线程写入完毕
线程Thread- 1 写入数据完毕,等待其他线程写入完毕
所有线程写入完毕,继续处理其他任务...
所有线程写入完毕,继续处理其他任务...
所有线程写入完毕,继续处理其他任务...
所有线程写入完毕,继续处理其他任务...
|
从上面输出结果可以看出,每个写入线程执行完写数据操作之后,就在等待其他线程写入操作完毕。
当所有线程线程写入操作完毕之后,所有线程就继续进行后续的操作了。
如果说想在所有线程写入操作完之后,进行额外的其他操作可以为CyclicBarrier提供Runnable参数:
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
|
public class Test {
public static void main(String[] args) {
int N = 4 ;
CyclicBarrier barrier = new CyclicBarrier(N, new Runnable() {
@Override
public void run() {
System.out.println( "当前线程" +Thread.currentThread().getName());
}
});
for ( int i= 0 ;i<N;i++)
new Writer(barrier).start();
}
static class Writer extends Thread{
private CyclicBarrier cyclicBarrier;
public Writer(CyclicBarrier cyclicBarrier) {
this .cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
System.out.println( "线程" +Thread.currentThread().getName()+ "正在写入数据..." );
try {
Thread.sleep( 5000 );
System.out.println( "线程" +Thread.currentThread().getName()+ "写入数据完毕,等待其他线程写入完毕" );
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e){
e.printStackTrace();
}
System.out.println( "所有线程写入完毕,继续处理其他任务..." );
}
}
}
|
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
线程Thread- 0 正在写入数据...
线程Thread- 1 正在写入数据...
线程Thread- 2 正在写入数据...
线程Thread- 3 正在写入数据...
|