继上篇文章JAVA并发之多线程基础(3)谈到的信号量以及读写锁以外,接下来就继续谈及JDK中并发类的操做。java
倒计时器是在某一些程序须要前置处理的时候完美搭档。例如咱们常常玩的手游端,在游戏开始以前,它会去调用其余的组件,例如画面环境、人物图像、武器装备等,等加载完成以后再进入到主界面中进行游戏。多线程
countDown()
方法是每一个线程完成以后减1,表明一个线程已经到达了终点线。咱们能够点击进去看到里面调用的方法:public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {//试图将一个线程设置共享模式
doReleaseShared();//共享模式下的释放操做
return true;
}
return false;
}
复制代码
await()
是每一个线程要到达的终点线,先执行完成的线程需在此等待未完成任务的线程。直至当前的countDown数量到0。public final void acquireSharedInterruptibly(int arg) throws InterruptedException {
if (Thread.interrupted())//判断线程是不是中断
throw new InterruptedException();
if (tryAcquireShared(arg) < 0)//尝试在共享模式下获取
doAcquireSharedInterruptibly(arg);//在共享可中断模式下获取。
}
复制代码
demo:并发
package com.montos.lock;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchDemo implements Runnable {
static final CountDownLatchDemo demo = new CountDownLatchDemo();
static final CountDownLatch latch = new CountDownLatch(10);
@Override
public void run() {
try {
Thread.sleep(new Random().nextInt(10) * 1000);
System.out.println("checking....");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
ExecutorService exec = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
exec.submit(demo);
}
latch.await();
System.out.println("prepare is end");
exec.shutdown();
}
}
复制代码
栅栏是指每个线程在某一处等待集合,而后接下来继续执行又到了某一处进行集合等待。能够说是上面
CountDownLatch
的加强版。dom
package com.montos.lock;
import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierDemo {
public static class Soldier implements Runnable {
private String name;
private final CyclicBarrier cycli;
public Soldier(String name, CyclicBarrier cycli) {
super();
this.name = name;
this.cycli = cycli;
}
@Override
public void run() {
try {
cycli.await();//十个线程一块儿等待集合
doSometing();
cycli.await();//十个线程一块儿等待事情处理完毕
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
private void doSometing() {
try {
Thread.sleep(new Random().nextInt(5) * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + "任务完成");
}
}
public static class BarrierRun implements Runnable {
boolean flag;
int N;
public BarrierRun(boolean flag, int n) {
super();
this.flag = flag;
N = n;
}
@Override
public void run() {
if (flag) {
System.out.println("士兵" + N + "任务完成");
} else {
System.out.println("士兵" + N + "集合完毕");
flag = true;
}
}
public static void main(String[] args) {
final int N = 10;
Thread[] allSoldier = new Thread[10];
boolean flag = false;
//N个线程到达以后,须要处理什么事情(这里是一块儿到达以后,处理BarrierRun事件)
CyclicBarrier cycli = new CyclicBarrier(N, new BarrierRun(flag, N));
System.out.println("集合队伍");
for (int i = 0; i < 10; i++) {
System.out.println("士兵" + i + "报道!");
allSoldier[i] = new Thread(new Soldier("士" + i, cycli));
allSoldier[i].start();
}
}
}
}
复制代码
经过上面一个小的demo,能够看到
CyclicBarrier
的用法。其中主要的就是await()
方法。寻找到里面去。主要的逻辑就是:ide
for (;;) {
try {
if (!timed)
trip.await();//调用重入锁中的condition进行当前线程等待
else if (nanos > 0L)
nanos = trip.awaitNanos(nanos);//时间限制等待
} catch (InterruptedException ie) {
if (g == generation && ! g.broken) {
breakBarrier();//将当前的屏障一代设置为断开并唤醒全部人
throw ie;
} else {
Thread.currentThread().interrupt();//当前线程中断
}
}
if (g.broken)
throw new BrokenBarrierException();
if (g != generation)
return index;
if (timed && nanos <= 0L) {
breakBarrier();
throw new TimeoutException();
}
}
复制代码
这上面就是对对应方法中的一个讲解。里面也用到了重入锁。经过上面的demo也知道
CyclicBarrier
与CountDownLatch
用法,总的来讲对于一些须要作以前准备的程序来讲,他们是最佳的选择。post