Sequential event processing via executorservice?

Executors. NewCachedThreadPool() will create new threads on demand, so it's not what you want. You want something like Executors.

NewSingleThreadExecutor() which will process the events one at a time, and queue up the rest.

Executors. NewCachedThreadPool() will create new threads on demand, so it's not what you want. You want something like Executors.

NewSingleThreadExecutor(), which will process the events one at a time, and queue up the rest. See javadoc: Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time.

– user384706 Nov 5 '10 at 8:40 1 @user384706: Only if your runnable doesn't throw exceptions. If it does, the thread will die, and a new one will be started. The only guarantee is that only one thread will be executing your tasks at a time.

– skaffman Nov 5 '10 at 8:49.

I think Executors. NewSingleThreadExecutor() and the submit() Method are the solution to your problem: download.oracle.com/javase/1.5.0/docs/ap....

Same need, and I've solved it! Please take a look at my question here : Java Runnable Queue.

Nicolas:Thank you. But doesn't the usage of Executors. NewSingleThreadExecutor() and passing in the Runnables have the same result as the definition of RunnableQueue from your post?

– user384706 Nov 5 '10 at 9:30 @user there is a slight difference. Say you have 5000 Executor instances ( created by newSingleThreadExecutor() ). Say you queue a task in each one at the same time.

You got 5000 threads running. On the other hand, you can instantiate 5000 RunnableQueue on the same ThreadPool with a fixed count of thread. – Nicolas Repiquet Nov 5 '10 at 10:01 @Nicolas:May be I am confused on this, but my understanding is that the newSingleExecutor() returns an executor instance that reuses a single thread.

The runnable tasks passed in execute method are queued and the single thread inside the executorService processes each task, one at a time. So you would have 1 thread processing 5000 task in your example. Am I misunderstanding something?

– user384706 Nov 5 '10 at 10:57 @Nicolas:I did some testing in my code using the newSingleExecutor() and from the logs, I see by the tread id that now there is only 1 thread running in the pool of executor. What do you think? – user384706 Nov 5 '10 at 11:06 @user I'm not talking about 5000 tasks in one queue, I'm talking about 5000 queues with one task each :) – Nicolas Repiquet Nov 5 '10 at 11:42.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions