Java アプリケーションを C# に移植する作業の一部は、C# で同期メッセージ バッファーを実装することです。同期とは、スレッドがメッセージを読み書きするのに安全であることを意味します。
Java では、synchronized
メソッド および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#で同様のことを達成するにはどうすればよいですか?