0
我們可以在C#achive它如下 -如何在java中創建命名鎖?
void readFile(File file) { Mutex mutexForFile = null; bool mutexCreateFlag; // Return true/false based on whether mutext is already exist or it is created as part of current call mutexForFile = new Mutex(false, file.FullName.GetHashCode().ToString(), out mutexCreateFlag); if (!mutexCreateFlag) throw new Exception("File UsedByOtherProcess"); else { mutexForFile .WaitOne(); // synchronized access to resource ProcessFile(fileInfo: file); mutexForFile .ReleaseMutex(); } }
不要去想代碼的相關性,就例如我給
將財產以後這樣可以在Java中?
嗯,http://download.oracle.com/javase/6/docs/api/java/util/concurrent/locks/package-summary.html – Jeremy
有' Lock類將支持這種功能 –
親愛的傑里米你能給我一個例子(我是新來的Java)。 –