user3014901
user3014901

Reputation: 377

EDT, asynchronous, synchronous

When starting a new thread from EDT (Event Dispatcher Thread), the new thread will never be EDT, as there is only one EDT, right? I am asking as I see some code checks if (!SwingUtils.isDispatcherThread()), and I wonder why it needs this check?

My question is: when starting a new thread, what makes it synchronous (has to wait for the new thread to finish) or asynchronous (the new thread returns right away)? Accordingly how to start a synchronous or asynchronous thread ourselves?

Use the following as an example. When starting a non-EDT thread in EDT like the following:

public void actionPerformed(final ActionEvent e)
{
    final Runnable runnable = new Runnable()
    {
      @Override
      public void run()
      {
        //do some non-gui task. The task is not long-running, but 
        //could be blocked
        doTask();       
      }
    }; 

    new Thread(runnable).start();
  }
});

Is the non-EDT thread spawned from EDT synchronous or asynchronous? Should EDT UI be blocked if doTask() hangs?

If I have no control over doTask() and the method cannot be changed, what is a good way to deal with the situation that spawns a new thread and the new thread might hang? use Thread.join() in parent thread?

Upvotes: 2

Views: 900

Answers (2)

ControlAltDel
ControlAltDel

Reputation: 35011

  1. EDT: Yes, there is only 1 EDT. Any thread you make will not be the EDT
  2. Synchronous vs Asynchronous: Thread in java is a class like any other, but when you call start() it creates a new thread that runs whatever's in the run() method or in the Runnable.run method if a Runnable is supplied asynchronously.
  3. Method might block / never return: The key to this is to use a callback / listener... A method that gets called at some point in the processing that causes an update. There is no general solution to stopping a blocked thread, but 2 mechanisms that can work in many situations are (a)InputStream/Reader.close() if you're waiting for data on an inputstream or reader, or (b) Thread.interrupt

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691755

the new thread will never be EDT, as there is only one EDT, right?

right.

I am asking as I see some code checks "if (!SwingUtils.isDispatcherThread())", and I wonder why it needs this check?

Because sometimes, a method can be invoked from the EDT or from a background thread, and must act differently depending on the current thread.

when starting a new thread, what makes it synchronous (has to wait for the new thread to finish) or asynchronous (the new thread returns right away)?

When you start a new thread, the new thread always runs concurrently to the spawning thread. Unless you explicitely join (or use another synchronization mechanism), the spawning thread never waits for the spawned thread to finish.

Should EDT UI be blocked if doTask() hangs?

No, unless for example the spawned thread hangs while keeping a lock that the EDT tries to acquire.

If I have no control over doTask() and the method cannot be changed, what is a good way to deal with the situation that spawns a new thread and the new thread might hang? use Thread.join() in parent thread?

That would be even worse: now the EDT would also hang, and thus freeze the UI completely, until the hung thread stops hanging and terminates.

If you have a hanging thread, then fix the code it's executing so that it doesn't hang anymore. And if you can't fix it, then ask the person responsible for that code to fix it.

Upvotes: 2

Related Questions