Questions tagged [event-dispatch-thread]

The event dispatch thread, or EDT, is a special background thread which handles events from the Java GUI event queue. Swing and Android have different implementations but they are similar in concept.

This thread must never be stalled with some computationally intensive or otherwise slow task (like networking), as the GUI stops responding to user input until the call returns.

As Java GUI environments are not thread-safe, it is the only thread that can safely touch GUI components (instantiating and updating GUI components, including setting values for text fields and the like). If any other thread must do this, this should be done through a wrapper method that runs on the EDT. In Swing, the wrapper method is SwingUtilities.invokeAndWait or SwingUtilities.invokeLater. In Android, Activity.runOnUiThread serves the same purpose.

775 questions
68
votes
2 answers

Java Event-Dispatching Thread explanation

I've recently started learning and exploring the basics of GUI programming in Java. Having been programming for a while I have only done backend work or work and as a result the closest I've gotten to user interfaces is the command console…
linuscash
  • 1,105
  • 1
  • 9
  • 18
56
votes
5 answers

SwingUtilities.invokeLater

My question is related to SwingUtilities.invokeLater. When should I use it? Do I have to use each time I need to update the GUI components? What does it exactly do? Is there an alternative to it since it doesn't sound intuitive and adds seemingly…
FadelMS
  • 2,027
  • 5
  • 25
  • 42
54
votes
5 answers

What does SwingUtilities.invokeLater do?

What does SwingUtilities.invokeLater do? Is it just delaying the execution of a block of codes inside its run method? What is the difference between calling an action within the invokeLater function or simply calling it at the end of the thread we…
user633784
27
votes
3 answers

Static Thread Analysis: Good idea?

I help maintain and build on a fairly large Swing GUI, with a lot of complex interaction. Often I find myself fixing bugs that are the result of things getting into odd states due to some race condition somewhere else in the code. As the code base…
Mark Peters
  • 80,126
  • 17
  • 159
  • 190
20
votes
4 answers

DispatchQueue crashing with main.sync in Swift

Please explain to me why I am getting this crash? Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) in this DispatchQueue.main.sync { print("sync") } This is my code. override func viewDidLoad() { …
Sankalap Yaduraj Singh
  • 1,167
  • 2
  • 14
  • 32
20
votes
3 answers

How can I catch Event Dispatch Thread (EDT) exceptions?

I am using a class called MyExceptionHandler that implements Thread.UncaughtExceptionHandler to handle normal exceptions in my project. As I understand this class can't catch the EDT exceptions, so I tried to use this in the main() method to handle…
Brad
  • 4,457
  • 10
  • 56
  • 93
16
votes
2 answers

How do I know if I'm on the event dispatch thread?

1.Consider my code is on some line of a JPanel that I have, am I automatically on EDT? 2.Same question for all other classes which are not belong to GUI, JPanels or other view classes, simple logical class. 3.If I have JPanel which I'm playing…
JavaSa
  • 5,813
  • 16
  • 71
  • 121
15
votes
2 answers

Infinite loop in EventQueue.isDispatchThread()

I have a Java program taking 100% cpu, but seemingly doing nothing. If I take a thread dump, there are 4 threads (out of a pool of 5) waiting to take a lock. "Incoming WorkPool 5" - Thread t@363 java.lang.Thread.State: WAITING at…
Brecht Yperman
  • 1,209
  • 13
  • 27
15
votes
3 answers

When exactly is the Event Dispatch Thread started?

When exactly is the EDT started? What line of code is responsible of it? My guess is that "someSwingComponent.setVisible(true)" does the trick, but I'm not sure. Thanks!
Pieter
  • 777
  • 5
  • 17
14
votes
2 answers

How to stop the Swing EDT

The typical Swing application starts the EDT at the beginning and when the last window is closed the Application stops basically with a System.exit either implicit or explicit. But my little application is actually a plugin for a framework which…
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
14
votes
3 answers

Make a swing thread that show a "Please Wait" JDialog

The problem is this: I've a swing application running, at a certain point a dialog requires to insert username and password and to press "ok". I would like that when the user press "ok" the swing application does in this order: Open a "Please…
user2572526
  • 1,219
  • 2
  • 17
  • 35
14
votes
4 answers

Java Swing - running on EDT

I have a couple of questions with regards to Swing and using EDT for GUI updates. I just started reading on this stuff so I am a full beginner in this area: Which operations are required to run on the EDT? If they don't, is simply an Exception…
Bober02
  • 15,034
  • 31
  • 92
  • 178
13
votes
3 answers

Is the Swing repaint() method still safe to use outside the EDT in Java 7+?

I know that it used to be considered safe to call repaint() and a few other selected methods from any thread even with Swing's threading model, however I was recently told in a comment that this is not so. Google found a lot of older discussion…
Tim B
  • 40,716
  • 16
  • 83
  • 128
12
votes
3 answers

How to insert an event to the beginning of Event Dispatch Thread queue in java?

I already know how Event Dispatch thread works. If there be short and long events in Event Dispatch thread like below, the application can't be responsive. For the Sake of responsiveness in Swing, Event Dispatch thread should only be used for…
Hamed
  • 2,084
  • 6
  • 22
  • 42
12
votes
3 answers

Does the EDT restart or not when an exception is thrown?

(the example code below is self-contained and runnable, you can try it, it won't crash your system :) Tom Hawtin commented on the question here: Why do people run Java GUI's on the Event Queue that: It's unlikely that the EDT would crash. Unchecked…
NoozNooz42
  • 4,238
  • 6
  • 33
  • 53
1
2 3
51 52