1

Many-a-times I think why Java forced us to use EDT while working with Swing? Won't it would be much easier to update Swing components from any Thread or in other words it can be multithreaded.

Indeed, SwingUtilities.invokeAndWait and SwingUtilities.invokeLater helps us to line up the Swing events, but, they won't be comming into picture if the GUI components can be updated from any other Thread.

And, have Java makers really forgotten to make Swing Thread-Safe?

What problem may arise if swing is Multithreaded? My Java program have "Frozen" a dozen of time beacuse of this :/

joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • 6
    See http://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html – Jon Skeet Jan 05 '13 at 12:46
  • @JonSkeet I never thought this question is deep as a well.`The problem of input event processing is that it tends to run in the opposite direction to most GUI activity.` This is the root cause I guess. Thanks for the link :) – joey rohan Jan 05 '13 at 12:57

1 Answers1

7

What problem may arise if swing is Multithreaded? Have Java makers really forgotten to make Swing Thread-Safe?

Many attempts have been made to create multithreaded GUIs but most have failed either because they did not work or they were overly complicated to use properly. A single threaded model is the standard and the simplest solution.

My Java program have "Frozen" a dozen of time beacuse of this

That is because you don't use Swing the way you should. The fact that it is single threaded does not prevent you from running long tasks on another thread (and you should do that to prevent freezing). You can read more about it in the Concurrency in Swing Tutorial. In particular:

Careful use of concurrency is particularly important to the Swing programmer. A well-written Swing program uses concurrency to create a user interface that never "freezes" — the program is always responsive to user interaction, no matter what it's doing. To create a responsive program, the programmer must learn how the Swing framework employs threads.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783