2

I've been reading a bit about concurrency (which gives me a headache).

I understand you can set a task to run on the EDT from the main thread using:

SwingUtilities.invokeLater

but can you set a task to run on the main thread from the EDT?

As in:

Thread mymainthread=Thread.currentThread();//<referring to the thread that initially kicks off public static void main

public void mousePressed(MouseEvent e){ //queue a task to run on mymainthread }

Can it be done? Is it a bad idea?

The other question on SO similiar to this (here) talks about creating a new thread but wouldn't it be safer and simpler to keep using the main if I was aiming for a single thread (+EDT) application? .......or maybe I've got this all wrong.

EDIT: What I should have explained: I wanted to create objects that would communicate with each other on the main thread (running in a slow loop) so I didn't want any of them be instantiated on a different thread, edt or otherwise.

Community
  • 1
  • 1
Nikki
  • 3,664
  • 2
  • 15
  • 14
  • Why would you want to do that? Can you tell us more about the motivation behind your request? Myself, I think that you've got this all wrong. :-) There's nothing wrong with creating background threads, as long as you don't create hundreds of them. If you're worried about that scenario, then use a ThreadPool of some sort. – Hovercraft Full Of Eels Nov 03 '12 at 22:28
  • @HovercraftFullOfEels It seems to be a common problem with my questions. I was thinking that if I were creating objects that were frequently communicating with each other, wouldn't it be wiser to make sure they're all made on the same thread? – Nikki Nov 03 '12 at 22:31
  • 1
    Nikki: no, I think that perhaps it's better to strive to make sure that the communication is done in a thread-safe manner. – Hovercraft Full Of Eels Nov 03 '12 at 22:34
  • all Mouse and Key events by default are dispatched on EDT, but depends of target from these events / listeners, whats "main" ??? – mKorbel Nov 03 '12 at 22:45
  • @mKorbel Sorry about the lack of details. I updated my question a bit more. I was referring to the thread that starts the application. (before the gui is created). I wanted to keep it and use it as the backbone of the program and have the EDT do as little as possible – Nikki Nov 03 '12 at 23:02

2 Answers2

5

but can you set a task to run on the main thread from the EDT?

I think you are confused on what EDT is. Swing and many other frameworks use a technique called thread-confinement.
In order to guarantee thread-safety, all actions are executed from a single thread. This thread in Swing is called Event Dispatcher Thread.
This thread has a queue and executes all tasks from that queue sequentially one at a time, at the same thread. This is why your tasks should be short in order not to block the UI.
So when you use EDT you are essentially passing a task to its queue from your thread and EDT will eventually execute it.
What you can do is put a task on the EDT queue which spawns a thread to be executed on separate thread. If you want to use your current thread for some reason as the background thread perhaps you could but why would you need that? The most straightforward way is just to submit a runnable to run as part of some background thread e.g. part of a pool

Cratylus
  • 52,998
  • 69
  • 209
  • 339
2

You can create your own event loop to do thread-confinement. This would allow you a separate single thread which would behave like the EDT. Be careful not to share [effectively] mutable objects between the two threads simultaneously.

Implementation can be as simple as a while loop with a BlockingQueue. You can go slightly higher level by getting an ExecutorService from java.util.concurrent.Executors.newFixeThreadPool(1).

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305