mu_tou_man

导航

 

CountDownLatch(闭锁,有译倒计数,锁寄存):

public class CountDownLatchTest {

/**
* 比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了
* @param args
*/
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();
}
}
}


CyclicBarrier(回环栅栏,有译循环栅栏):

public class CyclicBarrierTest {

/**
* 通过它可以实现让一组线程等待至某个状态之后再全部同时执行,如:蓄水泄洪。我们可以用CyclicBarrier来实现。
*/

public static void main(String[] args) {
int N = 4;
CyclicBarrier barrier = new CyclicBarrier(N);
for (int i = 1; i <= N; i++)
new Runner(barrier).start();
}

static class Runner extends Thread {
private CyclicBarrier cyclicBarrier;

public Runner(CyclicBarrier cyclicBarrier) {
this.cyclicBarrier = cyclicBarrier;
}

@Override
public void run() {
try {
System.out.println("线程" + Thread.currentThread().getName() + "准备就位");
cyclicBarrier.await();
System.out.println("线程" + Thread.currentThread().getName() + "开始跑。");
Thread.sleep(5000);
System.out.println("线程" + Thread.currentThread().getName() + "到达终点。");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}

}
}
}

Semaphore(信号量):

public class SemaphoreTest {
/**
* 假设一个工厂有8个工人,只有5台机器,一台机器同时只能被一个工人使用,只有使用完了,其他工人才能继续使用。那么我们就可以通过Semaphore来实现
*
* @param args
*/
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(5); //机器数目
int workerNum = 8; //工人数
for (int i = 1; i <= workerNum; i++)
new Worker(i, semaphore).start();
}

static class Worker extends Thread {
private int num;
private Semaphore semaphore;

public Worker(int num, Semaphore semaphore) {
this.num = num;
this.semaphore = semaphore;
}

@Override
public void run() {
try {
semaphore.acquire();
System.out.println("工人" + this.num + "占用一个机器在生产...");
Thread.sleep(2000);
System.out.println("工人" + this.num + "释放出机器");
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


Exchanger(交易):
public class ExchangerTest {

/**
* 两个人约定交易地点,交易必须完成,不见不散,可以用exchanger来完成。
* @param args
*/
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
final Exchanger exchanger = new Exchanger();
Runnable runnable1 = new Runnable() {
@Override
public void run() {
try {
String data1 = "data one";
System.out.println("线程" + Thread.currentThread().getName() + "正在把数据" + data1 + "换出去");
Thread.sleep((long) (Math.random() * 1000));
String data2 = (String) exchanger.exchange(data1);
System.out.println("线程" + Thread.currentThread().getName() + "换回的数据为" + data2);
} catch (Exception e) {
System.out.println(e);
}
}
};

Runnable runnable2 = new Runnable() {
@Override
public void run() {
try {
String data1 = "data two";
System.out.println("线程" + Thread.currentThread().getName() + "正在把数据" + data1 + "换出去");
Thread.sleep((long) (Math.random() * 10000));
String data2 = (String) exchanger.exchange(data1);
System.out.println("线程" + Thread.currentThread().getName() + "换回的数据为" + data2);
} catch (Exception e) {
System.out.println(e);
}
}
};
service.execute(runnable1);
service.execute(runnable2);
}


}

posted on 2016-09-09 09:38  mu_tou_man  阅读(126)  评论(0编辑  收藏  举报