【发布时间】:2012-01-10 21:48:44
【问题描述】:
我最近有a problem with two threads sticking in deadlock because they weren't monitoring the same object the way I thought they were。事实证明,实现了单例模式solved the problem。 但是为什么呢?
我只实例化了一个对象是私有属性的类的实例,所以我希望它无论如何都是有效的单例。
为了问题的完整性,这里还有一些说明差异的代码:
在实现单例模式之前:
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) 在不同的线程中给出了不同的结果。
单例模式:
class Worker {
private static BlockingQueue q;
private BlockingQueue getQueue() {
if(q == null) {
q = new LinkedBlockingQueue();
}
return q;
}
// The rest is unchanged...
}
突然间,它起作用了。为什么这里需要这种模式?
【问题讨论】:
-
鉴于这是不可编译的代码,因此很难确定您的实际实现中可能发生了什么。
-
您在此处显示的任何内容都不会产生您所描述的情况。
-
他在这里发布了 Server 和 Worker 代码:pastebin.com/VZLUH2DT 当然,这里发布的线程启动代码也有帮助。
-
您也不应该使用单例模式(即
static限定符),因为您不能拥有多个工人。如果您尝试使用多个 Worker,那么他们都将使用相同的queue,这将毫无意义,除非在一些奇怪的人为情况下。 :-)
标签: java multithreading singleton