You can save one instance in a static field of the class, in which any number of threads can wait on. If the instance is stored, it does not matter. The key is that all threads have access to a single instance - whether from a static field / method or from a single service object or a static local variable.
public class MakeMeWait { private static Object semaphore = new Object(); public static void waitPlease() { semaphore.wait(); } public static void wakePlease() { semaphore.notifyAll(); } }
This example uses the built-in Java methods wait and notifyAll , you are much better off using java.util.concurrent instead of rolling your own multi-threaded solutions.
source share