I want to implement a queue processor. I'm planning to use a thread that will process the elements of queue one by one till that queue gets empty. The thread will stop once the queue is empty. Once the current processor thread is stopped, and a new element is added to the queue I want to start a new instance of the queue processor thread that will run till the queue gets empty again. This process should continue.
How can I implement a queue processor like this? How can I make sure that the old instance has stopped when I create a new one?
I'm starting a new instance of processor thread like this. Is that enough?
if (packetQueueProcessor == null || !packetQueueProcessor.isAlive()) {
packetQueueProcessor = new PacketQueueProcessor(this, packetQueue);
packetQueueProcessor.start();
}