我最近有a problem with two threads sticking in deadlock because they weren't monitoring the same object the way I thought they were。事实证明,实施Singleton模式solved the problem。 但是为什么?为什么我需要这个多线程应用程序中的Singleton模式?
我只实例化了其中一个对象是私有属性的类的一个实例,所以我期望它实际上是单实例。
对于这个问题的完整起见,在这里也说明差异的一些代码:
Singleton模式开始实施前:
class Worker {
private BlockingQueue q = new LinkedBlockingQueue();
public void consume(String s) {
// Called by thread 1.
// Waits until there is anything in the queue, then consumes it
}
public void produce(String s) {
// Called by thread 2.
// Puts an object in the queue.
}
// Actually implements Runnable, so there's a run() method here too...
}
细丝却开始是这样的:
Worker w = new Worker();
new Thread(w).start();
// Producer also implements Runnable. It calls produce on its worker.
Producer p = new Producer(w);
new Thread(p).start();
现在,当我检查实际上是队列用于produce()
和consume()
,System.identityHashCode(q)
在不同的线程中给出了不同的结果。
随着Singleton模式:
class Worker {
private static BlockingQueue q;
private BlockingQueue getQueue() {
if(q == null) {
q = new LinkedBlockingQueue();
}
return q;
}
// The rest is unchanged...
}
突然,它的工作原理。为什么这里需要这种模式?
由于这不是可编译的代码,因此很难确定您的实际实施中可能发生了什么。 – Perception 2012-01-10 21:56:17
你在这里显示的任何东西都不会产生你描述的情况。 – 2012-01-10 21:58:19
他在这里发布了服务器和工作者代码:http://pastebin.com/VZLUH2DT当然,在这里发布的线程启动代码也有帮助。 – jbindel 2012-01-10 22:01:13