关于使⽤CyclicBarrier使主线程等待⼦线程执⾏完之后再向下
执⾏的问题
CyclicBarrier
线程障碍,其实就是为线程制作⼀个集合点,相关知识⽹上⼀堆。
这次主要记录⼀下使⽤CyclicBarrier遇到的⼀点⼩问题。
需求:在主线程中创建两个⼦线程,希望能在⼦线程执⾏完成之后再执⾏主线程中的剩余代码。
代码⼀:
package com.iteye.wwwcomy.thread;
import urrent.BrokenBarrierException;
import urrent.CyclicBarrier;
import urrent.ExecutorService;
中文域名有用吗
import urrent.Executors;
public class RaceCondition implements Runnable {
Counter counter;
final static CyclicBarrier cb = new CyclicBarrier(2, new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "执⾏");
}
});
RaceCondition() {
}
RaceCondition(Counter counter) {
}
/**
* @param args
* @throws Exception
*/
正月里来正月正
public static void main(String[] args) throws Exception {
Counter counter = new Counter();
ExecutorService rvice = wCachedThreadPool();
Thread t1 = new Thread(new RaceCondition(counter));
手机行业报告
Thread t2 = new Thread(new RaceCondition(counter));
rvice.shutdown();
System.out.unt);
}
@Override
public void run() {
for (int i = 0; i < 5000000; i++)
counter.add(1);
try {
System.out.println("before");
cb.await();
System.out.println("complete");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
e.printStackTrace();
} finally {
}
}
}
class Counter {
/**
* 此处加上volatile并没有起到预期作⽤,依旧要把⽅法同步
* NOTICE 这⾥count的值的范围是多少?
* 循环⼗次的极限情况:
* 1.线程⼀拿到了0,等待线程⼆
* 2.线程⼆拿到了0,并且计算了9次,得到结果9并且写了回去
* 3.线程⼀第⼀次计算完,并且把1写⼊
* 4.线程⼆第⼗次取到了1
* 5.线程⼀计算完成,把结果写⼊
* 6.线程⼆把第⼗次的计算结果2写⼊
*/
protected volatile long count = 0;
public void add(long value) {
怀孕初期能喝茶吗unt++;
}
1984属什么}
结果发现: 主线程依旧在⼦线程之前执⾏到了。
再看代码⼆:
package com.iteye.wwwcomy.thread;
import java.util.ArrayList;
import urrent.Callable;
import urrent.CyclicBarrier;
import urrent.ExecutorService;
import urrent.Executors;
public class RaceConditionWithCyclicBarrier implements Callable<Boolean> { Counter2 counter;
static CyclicBarrier cb = null;
RaceConditionWithCyclicBarrier() {
}
RaceConditionWithCyclicBarrier(Counter2 counter) {
}
/
**
* @param args
* @throws Throwable
*/
public static void main(String[] args) throws Throwable {
灾难近义词Counter2 counter = new Counter2();
cb = new CyclicBarrier(2, new Runnable() { // 等到线程到达后执⾏⼀个后续task @Override
public void run() {
System.out.println(Thread.currentThread().getName() + "a");
}
});
});
ArrayList<Callable<Boolean>> listCall = new ArrayList<Callable<Boolean>>();
listCall.add(new RaceConditionWithCyclicBarrier(counter));
listCall.add(new RaceConditionWithCyclicBarrier(counter));
电能的单位是什么ExecutorService executor = wFixedThreadPool(2); // 必须是allThread的个数
try {
executor.invokeAll(listCall);
} catch (Throwable e) {
executor.shutdown();
throw e;
} finally {
System.out.println("over");
}
executor.shutdown();
// 这句是在所有线程都跑完之后才会执⾏
System.out.unt);
}
@Override
public Boolean call() throws Exception {
for (int i = 0; i < 500000; i++)
counter.add(1);
cb.await();
return true;
}
}
class Counter2 {
/**
* 此处加上volatile并没有起到预期作⽤,依旧要把⽅法同步
* NOTICE 这⾥count的值的范围是多少?
* 循环⼗次的极限情况:
* 1.线程⼀拿到了0,等待线程⼆
* 2.线程⼆拿到了0,并且计算了9次,得到结果9并且写了回去
* 3.线程⼀第⼀次计算完,并且把1写⼊
* 4.线程⼆第⼗次取到了1
* 5.线程⼀计算完成,把结果写⼊
* 6.线程⼆把第⼗次的计算结果2写⼊
*/
protected volatile long count = 0;
public void add(long value) {
}
}
竟然发现这次主线程在executor.invokeAll(listCall);后被挂起了,直到⼦线程执⾏完成之后才执⾏。
其实就是使⽤Runnable和Callable之后executor的API使⽤不同⽽已,怎么结果差距这么⼤。
断个点,在visualVM⾥⾯dump了⼀下线程发现Callable的executor执⾏代码中将主线程挂起了,见线程堆栈,具体源码按照堆栈进去跟就是了:
引⽤
"pool-1-thread-2" prio=6 tid=0x00000000067f7800 nid=0x1534 at breakpoint[0x000000000876f000]
java.lang.Thread.State: RUNNABLE
at com.iteye.wwwcomy.thread.RaceConditionWithCyclicBarrier.call(RaceConditionWithCyclicBarrier.java:60)
at com.iteye.wwwcomy.thread.RaceConditionWithCyclicBarrier.call(RaceConditionWithCyclicBarrier.java:1)
at urrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at urrent.FutureTask.run(FutureTask.java:138)
at urrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Locked ownable synchronizers:
- <0x00000007d5f2ca10> (a urrent.locks.ReentrantLock$NonfairSync)
"pool-1-thread-1" prio=6 tid=0x00000000067f6800 nid=0x1da4 at breakpoint[0x000000000866f000]
java.lang.Thread.State: RUNNABLE
at com.iteye.wwwcomy.thread.RaceConditionWithCyclicBarrier.call(RaceConditionWithCyclicBarrier.java:60)
at com.iteye.wwwcomy.thread.RaceConditionWithCyclicBarrier.call(RaceConditionWithCyclicBarrier.java:1)
at urrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at urrent.FutureTask.run(FutureTask.java:138)
at urrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Locked ownable synchronizers:
- <0x00000007d5f2c7d8> (a urrent.locks.ReentrantLock$NonfairSync)
"main" prio=6 tid=0x00000000002ae800 nid=0x168c waiting on condition [0x000000000251f000]
java.lang.Thread.State: WAITING (parking)党员个人查摆问题清单
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007d5f2c770> (a urrent.FutureTask$Sync)
at urrent.locks.LockSupport.park(LockSupport.java:156)
at urrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:811)
at
urrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:969) at
urrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1281) at urrent.FutureTask$Sync.innerGet(FutureTask.java:218)
at (FutureTask.java:83)
at urrent.AbstractExecutorService.invokeAll(AbstractExecutorService.java:205)
at com.iteye.wwwcomy.thread.RaceConditionWithCyclicBarrier.main(RaceConditionWithCyclicBarrier.java:46)
Locked ownable synchronizers:
- None
可见只有代码⼆的做法才能使主线程在⼦线程完成之后再执⾏,CyclicBarrier⾃⾝并不保证主线程在⼦线程完成之后执⾏。
CyclicBarrier仅仅是为调⽤await⽅法的线程设置⼀个集合点![/b]
[b]代码⼀的写法其实稍微做⼀点改变,也可以实现效果:
1.修改CyclicBarrier的初始线程数+1,
2.在主线程⾥⾯也加⼊ cb.await(); 就可以了
当时先写的代码⼆,所以有点先⼊为主了。。本来正在研究volatile,遇到这个问题先记录⼀下。
另外,CyclicBarrier的第⼆个参数,是使⽤最后⼀个执⾏完的线程执⾏的。