2

It is said that "Swing toolkit is not multithread-Safe?

What is meant by this statement?

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
  • http://stackoverflow.com/questions/2564388/javas-swing-threading – andersoj Mar 31 '11 at 11:37
  • http://stackoverflow.com/questions/1429681/what-happens-when-swings-threading-policy-is-violated – andersoj Mar 31 '11 at 11:37
  • 1
    possible duplicate of [Java: Swing Libraries & Thread Safety](http://stackoverflow.com/questions/182316/java-swing-libraries-thread-safety) – andersoj Mar 31 '11 at 11:38

1 Answers1

4

Check this link: http://www.jguru.com/faq/view.jsp?EID=131370

So to simplify the implementation of Swing library they chose it to be not thread safe. The argument being that most of the GUI related work happens in the callbacks from the GUI which happen on the single GUI thread anyways. Granted - for long running tasks the user will have to do more work if he/she wants to do multithreaded activity...

Specially the link given for the jfc.

And the this link: http://www.it.uu.se/edu/course/homepage/devgui/vt03/out/ThreadsAndSwing.pdf

After Swing components have been displayed on the screen, they should only be operated on by the event-handling thread. The event-handling thread (or just event thread) is started automatically by the Java VM when an application has a graphical interface. The event thread calls methods like paint() on Component, actionPerformed() on ActionListener, and all of the other event-handling methods


ADD

Check the second link I have given, specially the section: The Need for Worker Threads in a GUI Setting (also there is an example for explaining it). Quoting here for reference:

The event thread plays a critical role in an application with a graphical interface. Code that will be executed by the event-handling thread should be relatively brief and nonblocking. If the event-handling thread is blocked in a section of code for a while, no other events can be processed!

Favonius
  • 13,959
  • 3
  • 55
  • 95
  • @ Favonius why should not we have long running tasks at one event when working with swing? – Suhail Gupta Mar 31 '11 at 12:02
  • @Suhail Gupta: I have updated my answer. Please check it. Also look at the links given by @andersoj in response to your question. – Favonius Mar 31 '11 at 12:39