由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问题一枚
相关主题
哪位同修能帮我测试一下FMP 进驻 Programming 版
java concurrency时,你们用的callable还是runnable?Java 多线程 的架构如何改进?
重新学习Java Thread的Field变量与Thread Localscala开发效率确实奇高
编程题又一道一个multithread的问题(是不是有人觉的很简单?)
面试问题一问 (转载)java update main UI from child thread issue (转载)
python下的expect请教一个Java编程问题
how to debug multi-thread program?怎么练习multi-threading,平常工作都是用Java框架
multithread app的design要注意哪些问题?Java的多线程的一般问题
相关话题的讨论汇总
话题: integer话题: barrier话题: futuretask话题: runnable3
进入Programming版参与讨论
1 (共1页)
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. "

1 (共1页)
进入Programming版参与讨论
相关主题
Java的多线程的一般问题面试问题一问 (转载)
Java 多线程:还需要好CPU?python下的expect
[合集] can a single thread run into deadlock?how to debug multi-thread program?
c++多线程的工作面试一般会问哪些问题?multithread app的design要注意哪些问题?
哪位同修能帮我测试一下FMP 进驻 Programming 版
java concurrency时,你们用的callable还是runnable?Java 多线程 的架构如何改进?
重新学习Java Thread的Field变量与Thread Localscala开发效率确实奇高
编程题又一道一个multithread的问题(是不是有人觉的很简单?)
相关话题的讨论汇总
话题: integer话题: barrier话题: futuretask话题: runnable3