Event thread in java
events, java, multithreading
Solution
Sounds like a job for java.util.concurrent.ThreadPoolExecutor.
You can set this up with a "pool" of a single thread, and then throw tasks at it to execute for you.
Problem
I want to create a background Thread handling some tasks (Runnable) and executing them in the order in which they are posted. Important : these tasks must NOT be executed on the Event Dispatcher Thread. Something like : ``` BackgroundEventThread backgroundEventThread = new BackgroundEventThread(); ``` then, later, and in many places in the code: ``` Runnable thingToDo = new Runnable(){...}; backgroundEventThread.executeThis(thingToDo); //the things to do will be executed in the order in which they are posted. ``` The class `BackgroundEventThread` should be quite straightforward to code, but I was wondering if such a class already existed in some place unknown to me in the JDK or in some common library... EDIT : I don't know in advance the number of tasks to execute on this thread. I could have: - task0 (really short) happening at t0 - task1 (long to process...) happening at t0+1s - task2 (short) happening at t0+5s etc. - task3 (etc, etc...) And I need task2 (that I don't know in advance) to be executed after task1, and I want all these tasks to be executed asap. Exactly like tasks posted on the EDT, but NOT on the EDT.