我讀這個類代碼:同步(...)代碼塊上使用var = Thread.currentThread()
public class MultiThreadedServer implements Runnable {
// some more code
protected Thread runningThread = null;
public void run() {
synchronized(this) {
this.runningThread = Thread.currentThread();
}
// lots of code
}
}
這是什麼意思?線程本身被用作標誌來鎖定資源?我一點都不明白。
任何人都知道嗎?
'// blablahbla' - '//很多blahblahblah'的 - 這是一個不走我的。 'this'指向'MultiThreadedServer'的實例,而不是線程(它具有無效的標識符) –
我喜歡'protected Thread running = null;'的空間;' – Tschallacka
它不使用線程作爲鎖,它使用Runnable對象。給多個線程的是同一個對象嗎? Runnable中是否有其他方法可以同步? –