File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Threads with Swing and deadlock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads with Swing and deadlock" Watch "Threads with Swing and deadlock" New topic

Threads with Swing and deadlock

David Patterson
Ranch Hand

Joined: Jul 01, 2002
Posts: 65
Background: I am working on a very large Java/Swing application. We have many threads at work for a lot of our code. One component is a cache manager that remembers data retrieved from our server. If the network goes away, the cache manager needs to ask the user if they want to use the old data or cancel the transaction. I wrote a "choice manager" because we had several questions like this we needed to prompt the user for a decision.

The Choice Manager class has a synchronized method that does the prompting, or if the user has checked a previous "Don't ask me again" choice, just returns an action immediately. This method checks to see if it is on the EventDispatcherThread (EDT) and if so does the processing. If not, it does an invokeAndWait() to switch threads and prompt the user.

The deadlock happens right after a restart with the network disabled. Many components ask to reuse cached data at about the same time. My theory is that one of the slower of these components is running on the EDT and that thread is held up waiting for that component to get into my method. The code in the method is waiting for the EDT so it can do the Dialog to ask the user.

It seems the only solution is to make sure that none of the callers of this method are on the EDT to start with. Does that make sense?

Dave Patterson
patterd ~at~ comcast ~dot~ net
Henry Wong

Joined: Sep 28, 2004
Posts: 20530

As a first question, why is the method synchronized?

Since the method checks if it is the event dispatching thread before actually operating, there should not be a reason to worry about data conflicts right?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Patterson
Ranch Hand

Joined: Jul 01, 2002
Posts: 65

Thanks for the note. I made it synchronized so you would never get 8 popup dialog boxes at once (which might happen if the method was not synchronized). We figured it would annoy the users to get a stack of popups at once.

I guess the tradeoff is that with the stack of popups they would not get any hangs or deadlocks. Groan.

David Patterson
patterd1 ~at~ comcast ~dot~ net
I agree. Here's the link:
subject: Threads with Swing and deadlock
It's not a secret anymore!