0

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();
        }
  • 2
    Why is this not just one processor thread running round a producer-consumer queue? Why does the processor thread instance need to be continually terminated and created? – Martin James Aug 06 '19 at 18:58
  • @MartinJames I like that also. But I don't know how to implement that one. If you've any link or anything that can give an idea, please share it – Muhammed Thabjeel Aug 07 '19 at 05:26
  • Possible duplicate of https://stackoverflow.com/questions/1301691/which-concurrent-queue-implementation-should-i-use-in-java. I'm fairly convinced that you want one of those three queue types. You should read about each one. Once you've decided which one is appropriate, study how they're used. Or, if you have specific questions about how, you can ask here. – Jim Mischel Aug 08 '19 at 04:29

0 Answers0