Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Display.getDefault().syncExec(...) deadlock?
Display.getDefault().syncExec(...) deadlock? [message #660330] Thu, 17 March 2011 22:33 Go to next message
Eric Jain is currently offline Eric JainFriend
Messages: 266
Registered: July 2009
Senior Member
I have some code that often hangs the application when calling Display.getDefault().syncExec(...). Changing this to Display.getDefault().asyncExec(...) fixes that.

I wanted to figure out what threads are deadlocking, so I asked the Threading MBean's findDeadlockedThreads and findMonitorDeadlockedThreads, but both return null. The thread dump shows all threads RUNNABLE or TIMED_WAITING (on object monitor). There is no CPU usage, no network utilization, and no IO while the application is hanging, and the application has to be killed.

Is it correct to conclude that the deadlock must therefore be happening in native (i.e. SWT) code?
Re: Display.getDefault().syncExec(...) deadlock? [message #660369 is a reply to message #660330] Fri, 18 March 2011 07:24 Go to previous messageGo to next message
Praveen  is currently offline Praveen Friend
Messages: 86
Registered: July 2009
Member
Display.getDefault().syncExec() executes the runnable in the UI thread blocking the invoking thread until the runnable is executed.
If the UI thread is heavily processing other UI events or blocked on other purpose, then there is a chance that UI thread didn't get a chance to process the runnable. This might result much similar to the way that your application is hanged, though it might not theoretically.

Are you doing any UI activity during the problem scenario ? I don't think there could be any deadlock in native code of SWT, as the asyncExec functions pretty similar to syncExec with the exception that the invoking thread is not blocked.
Re: Display.getDefault().syncExec(...) deadlock? [message #660502 is a reply to message #660369] Fri, 18 March 2011 16:49 Go to previous messageGo to next message
Eric Jain is currently offline Eric JainFriend
Messages: 266
Registered: July 2009
Senior Member
Praveen wrote on Fri, 18 March 2011 03:24
Are you doing any UI activity during the problem scenario ? I don't think there could be any deadlock in native code of SWT, as the asyncExec functions pretty similar to syncExec with the exception that the invoking thread is not blocked.


Within the Runnable, I refresh a TableViewer and mark an editor as "dirty". It's possible that this is causing some kind of deadlock, but, as mentioned, none of the threads in the application appear to be deadlocked, and the CPU usage remains at 0.
Re: Display.getDefault().syncExec(...) deadlock? [message #660507 is a reply to message #660502] Fri, 18 March 2011 17:12 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1436
Registered: July 2009
Senior Member
If the code that is calling syncExec is running on the display thread, then this will cause the deadlock.

The Runnable passed to syncExec is placed on a queue. The display thread can't get to the queue until the current code executing on it returns. The syncExec call causes the current thread to wait. Since the Runnable can't be reached until your code returns and since your code won't return until the Runnable completes you have a deadlock.

Display thread calls your method. In your method you call syncExec which places a Runnable on the event queue. SyncExec waits for the Runnable to complete.

Since the thread is waiting, you see no CPU usage.

AsyncExec clears up the problem, because there is no wait. The asyncExec call returns, your method returns, the display thread then is able to read the event queue and process the Runnable from the asyncExec call.
Re: Display.getDefault().syncExec(...) deadlock? [message #660511 is a reply to message #660507] Fri, 18 March 2011 17:34 Go to previous message
Eric Jain is currently offline Eric JainFriend
Messages: 266
Registered: July 2009
Senior Member
David Wegener wrote on Fri, 18 March 2011 13:12
If the code that is calling syncExec is running on the display thread, then this will cause the deadlock [...]

Thanks for the explanation. However, I don't understand why calling syncExec from the display thread doesn't deadlock all the time? In fact, the following code never seems to deadlock:

Display.getCurrent().syncExec(new Runnable() {
    @Override
    public void run() {
        log.debug("=== sync exec ===");
        Display.getCurrent().syncExec(new Runnable() {
            @Override
            public void run() {
                log.debug("=== sync exec ===");
            }
        });
    }
});
log.debug("=== done ===");

[Updated on: Fri, 18 March 2011 17:35]

Report message to a moderator

Previous Topic:How to wrap text in a Label
Next Topic:Error when opening PDF file in eclipse browser with Adobe Reader X(10.0)
Goto Forum:
  


Current Time: Sat Apr 04 02:10:49 GMT 2020

Powered by FUDForum. Page generated in 0.02052 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top