多线程计数器之CountDownLatch和join
2016-08-31 16:34
387 查看
多线程访问全局变量,对同一全局变量的修改会影响其他线程变量的值。
多线程访问局部变量,对变量的修改不会影响其他线程中该变量的值。
/**
* 这个是实现计数器操作,主线程等待子线程全部完成
* 模拟场景: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!");
}
}
/**
* 示例线程类
*/
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使得主线程等待子线程完成。
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...");
}
}
多线程访问局部变量,对变量的修改不会影响其他线程中该变量的值。
/**
* 这个是实现计数器操作,主线程等待子线程全部完成
* 模拟场景: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!");
}
}
/**
* 示例线程类
*/
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使得主线程等待子线程完成。
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 多线程 day16 CountDownLatch 倒计时计数器
- Java多线程与并发库高级应用之倒计时计数器CountDownLatch
- 多线程进阶---Thread.join()/CountDownLatch.await() /CyclicBarrier.await()
- [Java并发]使用CountDownLatch和CyclicBarrier等待多线程完成
- JAVA对多线程的两个有用的辅助类(CountDownLatch和AtomicBoolean)
- 两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier
- 线程执行顺序——CountDownLatch、CyclicBarrier 、join()、线程池
- Java多线程——6 并发流程控制CountDownLatch、CycliBarrier
- Join,CountDownLatch,CyclicBarrier,Semaphore和Exchanger
- CountDownLatch理解一:与join的区别
- Java:多线程等待所有线程结束(CountDownLatch/CyclicBarrier)
- 试用CountDownLatch的副成品,多线程群发邮件小程序
- Java多线程20:多线程下的其他组件之CountDownLatch、Semaphore、Exchanger
- Java:多线程等待所有线程结束(CountDownLatch/CyclicBarrier) .
- Java多线程系列--“JUC锁”09之 CountDownLatch原理和示例 (r)
- Java多线程之~~~使用CountDownLatch来同步多个线程实现一个任务
- 两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier
- java多线程之CountDownLatch倒数闸门