Posts

Showing posts with the label java.util.concurrent

Is ThreadPoolExecutor's thread doesn't run concurrently with PriorityBlockingQueue

Is ThreadPoolExecutor's thread doesn't run concurrently with PriorityBlockingQueue I am using java ThreadPoolExecutor to run concurrent thread execution. I used ArrayBlockingQueue to keep threads in queue. But now requirement has changed and I need to add thread run time(no size limit) and it should be prioritized. So i decided to use PriorityBlockingQueue instead of ArrayBlockingQueue with some comparison Logic. After using PriorityBlockingQueue, threads are running sequentially one after one not concurrently. Only one thread run at a time, rather than whatever the active thread count will be. Please let me know if anybody have any suggestions to resolve this issue and achieve my requirement(thread should be added in pool at run time and it execution should be based on priority). My demo code: //RejectedExecutionHandler implementation RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl(); //Get the ThreadFactory implementation to use Bl...

Can ConcurrentLinkedDeque have a fixed size and overwrite old elements?

Can ConcurrentLinkedDeque have a fixed size and overwrite old elements? If I got this right ConcurrentLinkedDeque acts as a stack right if you use pollLast() ? pollLast() Now my issue is that I need a set size of ConcurrentLinkedDeque. My producer doesn't stop, so even if I have 16GB of ram I will run out eventually. SO is it possible to se a fixed size ? My implementation: ConcurrentLinkedDeque<String> queue = new ConcurrentLinkedDeque<>(); Producer (Thread 1): runs queue.add(line); Consumer (Thread 2): runs queue.pollLast(); queue.add(line); queue.pollLast(); Please note that both threads run in a while true loop . This is because of the requirements. That is why I am using ConcurrentLinkedDeque and not ArrayBlockingQueue or SynchronousQueue because it is non-blocking. ConcurrentLinkedDeque ArrayBlockingQueue SynchronousQueue Also do I need to declare anything synchronised ? synchronised Use a fixed size BlockingQueue which would all...