我最近有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