How can I create an event-driven background thread in Java?

I like the simplicity of invokeLater()sending units of work to AWT EDT. It would be nice to have a similar mechanism for sending work requests to the background thread (for example, SwingWorker), but, as I understand it, they do not have a mechanism for managing the event queue and dispatch, on which invokeLater () depends.

So, instead, I ended up having my background thread block the queue to which other threads are sending messages, and the thread essentially starts a receive cycle, blocking until the message arrives.

In fact, perhaps this is how it would be possible to implement behavior similar to EDT in the background thread (or would it be?). On the other hand, I like the simplicity of the stream, which is simply inertia inertially wrapped around, processing the "working drops" whenever they are sent to it from some invisible queue for dispatching events in the sky. Does Java provide a way to create such an "event-driven workflow"? Or, in the end, is it the queue? And in a related way, are there any flaws in the invokeLater()messaging method ?

+5
source share
2 answers

java.util.concurrent, Executor, : executor.execute(runnableTask);. , , : executor = Executors.newSingleThreadExecutor()'. ExecutorService, , .

+1

Producer-Consumer Design Patter ( , ) - ; EDT . , .

  • "-" , , .
  • Worker, EDT, ( GUI).

, Producer-Consumer Worker, , . , , , - , , .

+3

All Articles