多线程计数器之CountDownLatch和join
2017-07-17 11:24
471 查看
[java] view
plain copy
print?
/**
* 这个是实现计数器操作,主线程等待子线程全部完成
* 模拟场景:10个人赛跑,当所有的人到达终点后,比赛结束
* @author zhangm
*
*/
public class TestCountDownLatch {
//计数器,从2开始计数
private final static CountDownLatch mCountDownLatch = new CountDownLatch(3);
/**
* 示例工作线程类
*/
private static class WorkingThread extends Thread {
private final String mThreadName;
private final int mSleepTime;
public WorkingThread(String name, int sleepTime) {
mThreadName = name;
mSleepTime = sleepTime;
}
@Override
public void run() {
System.out.println("[" + mThreadName + "] started!");
try {
Thread.sleep(mSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
mCountDownLatch.countDown();
System.out.println("[" + mThreadName + "] end!");
}
}
/**
* 示例线程类
16be9
*/
private static class SampleThread extends Thread {
@Override
public void run() {
System.out.println("[SampleThread] started!");
try {
// 会阻塞在这里等待 mCountDownLatch 里的count变为0;
// 也就是等待另外的WorkingThread调用countDown()
mCountDownLatch.await();
} catch (InterruptedException e) {
}
System.out.println("[SampleThread] end!");
}
}
public static void main(String[] args) throws Exception {
// 最先run SampleThread
new SampleThread().start();
// 运行工作线程
new WorkingThread("WorkingThread1", 1000).start();
new WorkingThread("WorkingThread2", 2000).start();
new WorkingThread("WorkingThread3", 1000).start();
}
}
执行结果如下:
[SampleThread] started!
[WorkingThread1] started!
[WorkingThread2] started!
[WorkingThread3] started!
[WorkingThread1] end!
[WorkingThread2] end!
[WorkingThread3] end!
[SampleThread] end!
另外还可以使用join使得主线程等待子线程完成。
[java] view
plain copy
print?
public class TestJoin extends Thread{
private String name;
private int time;
public TestJoin(String name,int time)
{
this.name=name;
this.time=time;
}
//子线程执行部分
@Override
public void run()
{
System.out.println(this.getName() + " staring...");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName() + " end...");
}
public static void main(String[] args)
{
System.out.println("main thread starting...");
List<TestJoin> list = new ArrayList<TestJoin>();
int time;
for (int i = 1; i <= 5; i++)
{
time=(int) (Math.random()*3+1);
TestJoin my = new TestJoin("Thrad " + i,1000*time);
my.start();
list.add(my);
}
try
{
for (TestJoin my : list)
{
my.join();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("main thread end...");
}
}
plain copy
print?
/**
* 这个是实现计数器操作,主线程等待子线程全部完成
* 模拟场景:10个人赛跑,当所有的人到达终点后,比赛结束
* @author zhangm
*
*/
public class TestCountDownLatch {
//计数器,从2开始计数
private final static CountDownLatch mCountDownLatch = new CountDownLatch(3);
/**
* 示例工作线程类
*/
private static class WorkingThread extends Thread {
private final String mThreadName;
private final int mSleepTime;
public WorkingThread(String name, int sleepTime) {
mThreadName = name;
mSleepTime = sleepTime;
}
@Override
public void run() {
System.out.println("[" + mThreadName + "] started!");
try {
Thread.sleep(mSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
mCountDownLatch.countDown();
System.out.println("[" + mThreadName + "] end!");
}
}
/**
* 示例线程类
16be9
*/
private static class SampleThread extends Thread {
@Override
public void run() {
System.out.println("[SampleThread] started!");
try {
// 会阻塞在这里等待 mCountDownLatch 里的count变为0;
// 也就是等待另外的WorkingThread调用countDown()
mCountDownLatch.await();
} catch (InterruptedException e) {
}
System.out.println("[SampleThread] end!");
}
}
public static void main(String[] args) throws Exception {
// 最先run SampleThread
new SampleThread().start();
// 运行工作线程
new WorkingThread("WorkingThread1", 1000).start();
new WorkingThread("WorkingThread2", 2000).start();
new WorkingThread("WorkingThread3", 1000).start();
}
}
执行结果如下:
[SampleThread] started!
[WorkingThread1] started!
[WorkingThread2] started!
[WorkingThread3] started!
[WorkingThread1] end!
[WorkingThread2] end!
[WorkingThread3] end!
[SampleThread] end!
另外还可以使用join使得主线程等待子线程完成。
[java] view
plain copy
print?
public class TestJoin extends Thread{
private String name;
private int time;
public TestJoin(String name,int time)
{
this.name=name;
this.time=time;
}
//子线程执行部分
@Override
public void run()
{
System.out.println(this.getName() + " staring...");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName() + " end...");
}
public static void main(String[] args)
{
System.out.println("main thread starting...");
List<TestJoin> list = new ArrayList<TestJoin>();
int time;
for (int i = 1; i <= 5; i++)
{
time=(int) (Math.random()*3+1);
TestJoin my = new TestJoin("Thrad " + i,1000*time);
my.start();
list.add(my);
}
try
{
for (TestJoin my : list)
{
my.join();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("main thread end...");
}
}
相关文章推荐
- 多线程计数器之CountDownLatch和join
- 并发工具类:等待多线程完成的CountDownLatch,和join的区别
- Java多线程与并发库高级应用之倒计时计数器CountDownLatch
- java 多线程 day16 CountDownLatch 倒计时计数器
- 多线程进阶---Thread.join()/CountDownLatch.await() /CyclicBarrier.await()
- Java多线程系列--“JUC锁”09之 CountDownLatch原理和示例
- 多线程之CountDownLatch和CyclicBarrier
- Java多线程编程-(6)-两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier
- 两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier
- java多线程编程之CountDownLatch
- Java多线程系列-多线程下的其他组件之CountDownLatch、Semaphore、Exchanger
- java多线程编程——同步器CountDownLatch(三)
- CountDownLatch理解一:与join的区别
- 多线程(十)CountDownLatch、CyclicBarrier和Semaphore
- 多线程(8)-同步辅助类CountDownLatch&CyclicBarrier& Semaphor
- 深谈多并发计数器CountDownLatch(一)
- 两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier
- 线程执行顺序——CountDownLatch、CyclicBarrier 、join()、线程池
- Join,CountDownLatch,CyclicBarrier,Semaphore和Exchanger
- (java)多线程之CountdownLatch的简单示例