Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What causes deadlock in GUI?  RSS feed

 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have been reading this book called Begining Java2 by Ivor Horton - Wrox publication. in the chapter for event handling the author says

because events are processed in a separate thread from the main thread, you
must not modify or query the GUI for an application from the main thread once the GUI has been displayed
or is ready to be displayed. Otherwise, a deadlock may result.


What does this mean ?

Further in the immideate next section "Avioding Deadlocks in GUI " after a few lines the author says

As I
said in the previous section, once an application GUI has been realized, modifying or querying it on the
main thread can cause deadlock because user interactions with the GUI, such as clicking a menu item,
are handled in the event-dispatching thread. There is also the rare possibility with some types of Swing
components that deadlocks can occur even when it is not apparent that you are modifying the GUI after
it has been realized. You can avoid any possibility of deadlock in your application arising from your GUI
creation code by arranging to execute all the code that creates the GUI on the event-dispatching thread.


I havent very much followed what the suthor is trying to say. I understand that he means to say that we cant change or modify the GUI one it has been displayed (rendered) but what is the problem with that ? Does that mean that we cant remove or add a component from within the actionPerformed() method corresponding to the component from the same window . Can anyone please throw some light on this fact



 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concurrency in Swing. In short, any update to the GUI should be done in the Event Dispatcher Thread (EDT) only. actionPerformed should be called in the EDT so you can add components in it.

There are a few exceptions like JTextArea.append() but these are explicitly documented that they can be called from other threads as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!