Forums Register Login

Threads with Swing and deadlock

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?

Henry
+Pie Number of slices to send: Send
Henry,

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've never won anything before. Not even a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 970 times.
Similar Threads
Please help explain the deadlock.
Andrew's Book
Handling EDT issues when listening to model events with Model View Presenter
Handling threading issues when listening to model events with ModelViewPresenter
Some questions on "Bodgitt and Scarper"
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:02:44.