You are describing the Producer / Consumer pattern. Java has thread safe ways to achieve this. The easiest would be to use java.util.concurrent.BlockingQueue<E>
The way to use it is to share the BlockingQueue between a Consumer (thread) and one or more Producer threads. The Consumer blocks with the method E Queue.take(). The clients add to the Queue with method Queue.offer(E).
First create the BlockingQueue:
BlockingQueue<MyObject> myQueue = new LinkedBlockingQueue<MyObject>(int capacity);
The server thread will look something like this:
BlockingQueue<MyObject> myQueue;
// A constructor which receives the BlockingQueue object
// Main Loop
boolean isInterrupted = false;
while(!isInterrupted) {
try {
MyObject object = myQueue.take(); // Will block
// Process object
} catch (InterruptedException e) {
isInterrupted = true;
}
}
And for each producer thread, pass the BlockingQueue object to each, and use:
BlockingQueue<MyObject> myQueue;
// A constructor which receives the BlockingQueue object
// Create MyObjects
myQueue.offer(myObject); // Will add to the queue
There is the concept of a queue capacity. If the producers add too quickly queue may reach capacity. An offer can fail in this case. See the javadoc LinkedBlockingQueue for full details
LinkedBlockingQueue. It will block