Empresa engajada
Two cooperative threads, how do you make one thread yield until certain condition is met.
Sigiloso
i would clarify whether we talk about cooperation vs. synchronization and yielding vs. blocking. the following code: while (!conditionIsMet()) { Thread.currentThread().yield(); } is bad because it leads to spinning. It may be only marginally better than just while (!conditionIsMet()); This is a standard approach: Thread waiting for condition: synchronized (guardObject) { while (!conditionIsMet()) { guardObject.wait(); } } thread modifying condition: synchronized (guardObject) { // the same object modifyCondition(); guardObject.notify(); // or notifyAll() depending on the code logic } But I'd rather describe it as threads coordination (not cooperation) via blocking (not yielding) using standard wait()/notify() primitives