• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What causes deadlock in GUI?

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic