blockingqueue

A bounded BlockingQueue that doesn't block

The title of this question makes me doubt if this exist, but still: I'm interested in whether there is an implemented of Java's BlockingQueue, that is bounded by size, and never blocks, but rather throws an exception when trying to enqueue too many elements. Edit - I'm passing the BlockingQueue to an Executor, which I suppose uses its ...

ScheduledExecutorService with variable delay

Suppose I have a task that is pulling elements from a java.util.concurrent.BlockingQueue and processing them. public void scheduleTask(int delay, TimeUnit timeUnit) { scheduledExecutorService.scheduleWithFixedDelay(new Task(queue), 0, delay, timeUnit); } How can I schedule / reschedule the task if the frequency can be changed dyna...

java BlockingQueue does not have a blocking peek?

I have a blocking queue of objects. I want to write a thread that blocks till there is a object on the queue. Similar to the functionality provided by BlockingQueue.take(). However, since I do not know if I will be able to process the object successfully, I want to just peek() and not remove the object. I want to remove the object only...

producer/consumer work queues

I'm wrestling with the best way to implement my processing pipeline. My producers feed work to a BlockingQueue. On the consumer side, I poll the queue, wrap what I get in a Runnable task, and submit it to an ExecutorService. while (!isStopping()) { String work = workQueue.poll(1000L, TimeUnit.MILLISECONDS); if (work == null) ...

BlockingQueue decorator that logs removed objects

I have a BlockingQueue implementation that's being used in a producer-consumer situation. I would like to decorate this queue so that every object that's taken from it is logged. I know what the straightforward implementation would look like: simply implement BlockingQueue and accept a BlockingQueue in the constructor to which all of the...

how to terminate retrieval from a blocking queue

Hi, I have some code where i execute a several tasks using Executors and a Blocking Queue. The results have to be returned as an iterator because that is what the application that i work on expects. However, there is a 1:N relationship between the task and the results added to the queue, so i cannot use the ExecutorCompletionService. W...

Reading from multiple BlockingQueues within a single thread

I have three Java's LinkedBlockingQueue instances and I'd like to read from them (take operation) only using one thread. The naive approach is to have one thread per queue. Is there anything like the UNIX select system call for blocking queues in Java? Thanks. ...

How to immediately release threads waiting on a BlockingQueue

Consider a BlockingQueue and a few threads waiting on poll(long, TimeUnit) (possibly also on on take()). Now the queue is empty and it is desired to notify the waiting threads that they can stop waiting. The expected behaviour is to have either null returned or the declared InterruptedException thrown. Object.notify() won't work for Li...

ThreadPoolExecutor policy

I'm trying to use a ThreadPoolExecutor to schedule tasks, but running into some problems with its policies. Here's its stated behavior: If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing. If corePoolSize or more threads are running, the Executor always prefers queuing a r...

ThreadPoolExecutor - ArrayBlockingQueue ... to wait before it removes an element form the Queue

I am trying to Tune a thread which does the following: A thread pool with just 1 thread [CorePoolSize =0, maxPoolSize = 1] The Queue used is a ArrayBlockingQueue: http://download.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html#poll%28long,%20java.util.concurrent.TimeUnit%29 Quesize = 20 BackGround: The thread tr...

Does ThreadPoolExecutor spawns a new thread if a current thread sleeps

Hi, This question is a followup on this one. Essentially what I am doing is declaring a ThreadPoolExecutor with just one thread. I am overriding the beforeExecute() method to put a sleep so that each of my tasks are executed with some delay among themselves. This is basically to give away the CPU to other threads since my thread is ki...