將Java應用程序移植到C#的一部分是在C#中實現同步的消息緩衝區。通過同步,我的意思是線程應該是安全的寫入和讀取消息。C#中的同步方法
在Java中,這可以使用方法和wait()
和和notifyAll()
來解決。
例子:
public class MessageBuffer {
// Shared resources up here
public MessageBuffer() {
// Initiating the shared resources
}
public synchronized void post(Object obj) {
// Do stuff
wait();
// Do more stuff
notifyAll();
// Do even more stuff
}
public synchronized Object fetch() {
// Do stuff
wait();
// Do more stuff
notifyAll();
// Do even more stuff and return the object
}
}
我怎樣才能實現在C#中類似的東西?
相關:http://stackoverflow.com/questions/541194/c-sharp-version-of-javas-synchronized-keyword – sshow 2013-02-22 15:31:02
@stigok無關,它是重複的 – 2013-02-22 15:35:14
不重複,他們沒有提及wait()和notifyAll() – Dimme 2013-02-22 15:40:02