N***m 发帖数: 4460 | 1 上帝造我的时候肯定是让我脑袋先着地的。
两个callable,一个返回100,一个返回200,
还有一个runnable3,用于cyclicbarrier的action.
程序运行到runnable3的System.out.println("barrier action executed!"),
似乎就悬在那里了。似乎在等什么,或者死循环?
[Main.java]
public class Main {
static List> tasks = new ArrayList
Integer>>();
public static void main(String[] args) throws InterruptedException,
ExecutionException {
CyclicBarrier barrier = new CyclicBarrier(2, new Runnable3()
);
Callable call = new Runnable1(barrier);
FutureTask task = new FutureTask(call);
tasks.add(task);
new Thread(task).start();
Callable call2 = new Runnable2(barrier);
FutureTask task2 = new FutureTask(call2);
tasks.add(task2);
new Thread(task2).start();
}
}
[Runnable1(2).java]
public class Runnable1 implements Callable {
private CyclicBarrier barrier;
public Runnable1(CyclicBarrier barrier) {
this.barrier = barrier;
}
...
public Integer call(){
...
barrier.await();
return 100;//or 200;
}
[Runnable3.java]
import java.util.concurrent.ExecutionException;
public class Runnable3 implements Runnable {
@Override
public void run() {
System.out.println("barrier action executed!");
int count = 0;
for(int i=0;i
try {
count+=Main.tasks.get(i).get();
System.out.println(Main.tasks.get(i).get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
System.out.println("count="+count);
}
} |
l********f 发帖数: 149 | 2 deadlock. I suspect the very last thread trip the barrier and trigger the
runnable3, which calls the task.get(). Get() call itself is a blocking call
. Thus that thread will be blocked and your app hangs.
Move ur runnable logic to main thread will fix the problem. |
N***m 发帖数: 4460 | 3 模模糊糊知道你想说什么,但是又不太确定。
main() {
// for thread 3?
Runnable r3 = new Runnable() {
public void run() {
doSomeWork();
}
}
}
call
【在 l********f 的大作中提到】 : deadlock. I suspect the very last thread trip the barrier and trigger the : runnable3, which calls the task.get(). Get() call itself is a blocking call : . Thus that thread will be blocked and your app hangs. : Move ur runnable logic to main thread will fix the problem.
|
l********f 发帖数: 149 | 4 Basically, ur runnable3 is blocked by Callable.Get(), and ur Callalbe thread
is not released by barrier until runnable3 is done. It's a deadlock
scenario.
"A CyclicBarrier supports an optional Runnable command that is run once per
barrier point, after the last thread in the party arrives, but BEFORE ANY
THREADS ARE RELEASED. This barrier action is useful for updating shared-
state before any of the parties continue. "
【在 N***m 的大作中提到】 : 模模糊糊知道你想说什么,但是又不太确定。 : main() { : // for thread 3? : Runnable r3 = new Runnable() { : public void run() { : doSomeWork(); : } : } : } :
|
N***m 发帖数: 4460 | 5 非常感谢!我看书太不仔细了。。。
thread
per
【在 l********f 的大作中提到】 : Basically, ur runnable3 is blocked by Callable.Get(), and ur Callalbe thread : is not released by barrier until runnable3 is done. It's a deadlock : scenario. : "A CyclicBarrier supports an optional Runnable command that is run once per : barrier point, after the last thread in the party arrives, but BEFORE ANY : THREADS ARE RELEASED. This barrier action is useful for updating shared- : state before any of the parties continue. "
|