- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个非常基本的线程池代码。它调用位于 linkedblockingqueue 中的工作对象池。该代码只是通过回收工作对象来打印出输入数据。
我发现与以下内容一致的死锁/卡住:
public class throttleheapthreadpool{
private quoteworkerobject[] channels;
private LinkedBlockingQueue<quoteworkerobject> idlechannels;
public throttleheapthreadpool(int poolsize,int stocks){
channels=new quoteworkerobject[poolsize];
idlechannels=new LinkedBlockingQueue<quoteworkerobject>();
for(int i=1;i<poolsize;i++){
channels[i]=new quoteworkerobject(idlechannels);
idlechannels.add(channels[i]);//All WORKERS to Idle pool to start
}
}
public void execute(Integer quote){
quoteworkerobject current = null;
try {
//extract worker from pool
current = (quoteworkerobject)idlechannels.take();
current.put(quote);
} catch (InterruptedException e) {
}
}
class quoteworkerobject{
LinkedBlockingQueue<Integer> taskqueue=new LinkedBlockingQueue<Integer>();
Thread quotethread=null;
LinkedBlockingQueue<quoteworkerobject> idle=null;
@SuppressWarnings("unchecked")
public quoteworkerobject(LinkedBlockingQueue<quoteworkerobject> idlechannels){
this.idle=idlechannels;
Runnable r=new Runnable(){
public void run() {
insertquote();
}
};
quotethread=new Thread(r);
quotethread.start();//spawn a thread from the worker
}
public void put(Integer quote){
taskqueue.add(quote);
}
public void insertquote(){
try{
Integer thisquote=taskqueue.take();
idle.add(this);
}
catch(Exception ex){
}
}
}
public static void main(String[] args){
throttleheapthreadpool pool=new throttleheapthreadpool(5,200);
Random randomGenerator = new Random();
for(int node=0;node < 20;node++){
int d=randomGenerator.nextInt(5*200);
pool.execute(d);
}
}
}
此代码在第 8 次执行时始终卡住 - 在该点current = (quoteworkerobject)idlechannels.take();
上面有什么问题吗?
最佳答案
这正是我(讨厌?)不喜欢使用此类代码的原因。您应该考虑让您/我们的生活更轻松,并编写即使在几个月后您也可以查看并证明它的代码:相应地为您的变量命名,编写简短的文档或解释等。我花了 25 分钟来重构,因为我不明白发生了什么。
我添加了一个小的重构,我也添加了一些断点,看看代码——解释在里面。但问题出在 insertQuote 方法中 - 它完成得太早了。
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
public class Pool {
private Worker[] workers;
private LinkedBlockingQueue<Worker> workerQueue;
/**
* Create a pool of 5 workers and a {@link LinkedBlockingQueue} to store them
*/
public Pool(int poolsize) {
//1. First you get here : you create a Pool of 5 Worker Threads and a Queue to store them
System.out.println("1.");
workers = new Worker[poolsize];
workerQueue = new LinkedBlockingQueue<Worker>();
for (int i = 0; i < poolsize; i++) {
//2. You instantiate 5 worker Threads and place each of them on the Queue
System.out.println("2.");
workers[i] = new Worker(workerQueue);
workerQueue.add(workers[i]);
}
}
public void execute(Integer quote) {
Worker current = null;
try {
// extract worker from pool
//6. Get a worker from the Queue
System.out.println("6.");
current = workerQueue.take();
current.put(quote);
} catch (InterruptedException e) {
}
}
/**
*
*
*/
class Worker {
LinkedBlockingQueue<Integer> taskqueueForEachWorker = new LinkedBlockingQueue<Integer>();
LinkedBlockingQueue<Worker> workerQueue = null;
public Worker(LinkedBlockingQueue<Worker> idlechannels) {
new Thread(new Runnable() {
@Override
public void run() {
//3. You call the insert quote method
System.out.println("3.");
insertquote();
}
}).start();
}
public void put(Integer quote) {
//7. Add a task for each Thread to do
System.out.println("7.");
taskqueueForEachWorker.add(quote);
}
//TODO The problem is here: After you execute this line : workerQueue.add(this); this method ends, NO MORE worker Threads are put on the queue,
// thus at point 6 you block, well because there are no more worker Threads an no one add them.
public void insertquote() {
try {
// 4. You try to take an Integer from the Pool of tasks from rach Thread, but there is nothing yet - it is empty, thus each Thread (worker)
// blocks here, waiting for a task
System.out.println("4.");
Integer thisquote = taskqueueForEachWorker.take(); // This will successed only after 7.
workerQueue.add(this);
} catch (Exception ex) {
}
}
}
public static void main(String[] args) {
Pool pool = new Pool(5);
Random randomGenerator = new Random();
for (int node = 0; node < 20; node++) {
int d = randomGenerator.nextInt(5 * 200);
System.out.println("5.");
pool.execute(d);
}
}
}
输出将为 1。2.3.4.2.3.4.2.3.4.2.3.4.2.3.4.5.6.7.5.6.7.5.6.7.5.6.7.5.6.7.5.6.
看到最后一行是 6。如果因为方法 insertQuote 已经退出而阻塞在这里,因此队列现在是空的,所有工作线程都已被占用。
在我看来,由于您的工作线程每个都使用单独的队列,因此您应该实现“工作窃取”模式或 Deque。也看看那个。
关于java - LinkedBlockingQueue 中的死锁(?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11832770/
我有类似下面的代码: ... id: myComponent signal updateState() property variant modelList: [] Repeater { mo
我正在处理一些我无法展示的私有(private)代码,但我已经制作了一些示例代码来描述我的问题: 主.c: #include #include #include #include typede
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: what are the differences in die() and exit() in PHP? 我想
在编写 Perl 模块时,在模块内部使用 croak/die 是一个好习惯吗? 毕竟,如果调用者不使用 eval block ,模块可能会使调用它的程序崩溃。 在这些情况下,最佳做法是什么? 最佳答案
我有一些搜索线程正在存储结果。我知道当线程启动时,JVM native 代码会代理在操作系统上创建新 native 线程的请求。这需要 JVM 之外的一些内存。当线程终止并且我保留对它的引用并将其用作
我刚刚花了很多时间调试一个我追溯到 wantarray() 的问题。 .我已将其提炼为这个测试用例。 (忽略 $! 在这种情况下不会有任何有用信息的事实)。我想知道为什么wantarray在第二个示例
我看到一些代码是这样做的: if(something){ echo 'exit from program'; die; } ...more code 和其他只使用 die 的人: if
我正在尝试将此表格用于: 如果任何 $_POST 变量等于任何其他 $_POST 变量抛出错误。 如果只有几个,那不是问题,但我有大约 20 个左右所以如果我想这样做,我将不得不像这样 但这
每次我运行: hadoop dfsadmin -report 我得到以下输出: Configured Capacity: 0 (0 KB) Present Capacity: 0 (0 KB) DFS
我是一名优秀的程序员,十分优秀!