リアルタイムのイベント メッセージを高速で処理するメソッドがあるとします。
各呼び出し (メッセージが届く) ごとに、追跡したい複数の状態があり、メソッドの次の呼び出しで実行する処理の種類は現在の状態によって異なります。
レートが高く、単一スレッドでの処理に時間がかかる可能性があるため、前の呼び出しが次の呼び出しの前に終了しない場合があります。
各メソッド呼び出しに非同期マルチスレッド実装 (スレッド プールなど) を使用すると、複数の呼び出しが同時に実行される可能性があり、それらのそれぞれが同じ状態に評価され、同じ種類の処理が発生します。私が欲しいものではありません。スレッド呼び出しの 1 つで変数の状態が変更された場合、他のスレッドがその状態を認識できるようにしたいと考えています。
私の質問は、レートと呼び出しごとの処理を非同期で処理することを確認したいが、同時に「同時に」スレッドへの複数の呼び出しが状態を認識していることを確認してください。順序はそれほど重要ではありません。
すなわち:
state = false;//current state
a thread b thread (and vice versa if thread b or thread a "saw" it first)
------------------------------
| |
| |
sees false sees false (should "see" true)
changes to true changes to true (should not change to true)
| |
void processMessage(String message) {
Runnable runner = new Runnable() {
void run() {
if(track.in_state == true) {
if(track.state == 1) {
track.in_state = false;
//do something here
}
else if(track.state == 2) {
track.in_state = false;
//do something here
}
}
}
}
poolA.executor(runner);
//what happens here is that multiple threads are executed with same processing here
}
void processADifferentMessage(String message) {//a different but also dependent on the state tracker object
Runnable runner = new Runnable() {
void run() {
if(track.in_state == false) {
//do something here
}
}
};
//I also want to make sure that its state aware here as well in this thread pool
poolB.executor(runner);
}
返信ありがとうございます。