Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to distinguish UI / non-UI thread in eclipse development
How to distinguish UI / non-UI thread in eclipse development [message #753957] Mon, 31 October 2011 08:05 Go to next message
Sai Missing name is currently offline Sai Missing nameFriend
Messages: 25
Registered: March 2011
Junior Member
[If this is the wrong place to ask the following question, please kindly guide me to the right place. It may be more related to general eclipse plugin questions]

As we know that, Invalid Thread Access is one of the most frequent bugs in eclipse plugins. Specially, if an UI element is touched by a non-UI thread, an SWTException: invalid thread exception will be thrown. Here is a typical example in JDT: https://bugs.eclipse.org/bugs/show_bug.cgi?id=125868

So, my question is that when writing or modifying eclipse plugin, how can I know the current code will be invoked by UI-thread or not? For the above example, how could I know in advance that "org.eclipse.core.internal.jobs.Worker.run(Worker.java:58)" will be invoked by non-UI thread? If I have a clear view of that, such bug can be avoided. I often encounter such bugs when modifying eclipse plugins, so really would like to receive suggestion and guidance.

A more general question is that: how eclipse framework initialize plugin classes, and invoke its methods? which classes will be invoked by UI thread, and which one will be invoked by non-UI thread? is that specified in xml file?

Thanks a lot, any suggest is highly appreciated.

-Sai
Re: How to distinguish UI / non-UI thread in eclipse development [message #753975 is a reply to message #753957] Mon, 31 October 2011 10:15 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
UI thread is the thread that creates the 'Display' i.e. the 'main'
thread (the thread which has the name 'main').

Create a breakpoint in the piece of code you want to know runs in UI
thread or not and then try to hit that breakpoint. You will know from
the Debug view if the code is being executed in 'main' thread or not.
Re: How to distinguish UI / non-UI thread in eclipse development [message #754080 is a reply to message #753975] Mon, 31 October 2011 16:48 Go to previous messageGo to next message
Sai Missing name is currently offline Sai Missing nameFriend
Messages: 25
Registered: March 2011
Junior Member
Thanks Deepak for your reply.

I am still a little bit confused at "how to identify UI thread from source code", i.e., given the source code of JDT, is it possible to tell which chuck of code is executed by UI thread, and which chuck of code (e.g., class, method) *may be* executed non-UI thread. (even not 100% precise). Since that error is so common, I even plan to write an automated program analysis tool to identify this potential errors (i.e., report all cases that a non-UI thread touches a UI element). So, would like to know your suggestion.

I entirely agree that UI thread is the thread that creates 'Display'. So, could I understand it in this way: all eclipse UI extensions (e.g., views, dialogs) are created by UI threads? and non-UI threads are spawned from UI thread to handle additional events?

For the confirmed JDT bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=125868

However, seems that through the whole stack trace, there is no "thread.start()" method. so, where does the non-UI thread come from? Did the eclipse framework spawn a non-UI thread for it? (which is not shown in the stack trace?)

Thanks a lot. Any suggestion are highly appreciated. I really would like to understand this problem.

-Sai



Deepak Azad wrote on Mon, 31 October 2011 06:15
UI thread is the thread that creates the 'Display' i.e. the 'main'
thread (the thread which has the name 'main').

Create a breakpoint in the piece of code you want to know runs in UI
thread or not and then try to hit that breakpoint. You will know from
the Debug view if the code is being executed in 'main' thread or not.

Re: How to distinguish UI / non-UI thread in eclipse development [message #754084 is a reply to message #754080] Mon, 31 October 2011 17:02 Go to previous messageGo to next message
Sai Missing name is currently offline Sai Missing nameFriend
Messages: 25
Registered: March 2011
Junior Member
More precisely, last stack trace shows:

at org.eclipse.core.internal.jobs.Worker.run(Worker.java:58)


Class Worker extends the Thread, and is invoked from anywhere else (invoking its run method). I am wondering How could I know which thread (and class) creates the Work object, and start the Worker thread?

Thanks

-Sai



Sai wrote on Mon, 31 October 2011 12:48
Thanks Deepak for your reply.

I am still a little bit confused at "how to identify UI thread from source code", i.e., given the source code of JDT, is it possible to tell which chuck of code is executed by UI thread, and which chuck of code (e.g., class, method) *may be* executed non-UI thread. (even not 100% precise). Since that error is so common, I even plan to write an automated program analysis tool to identify this potential errors (i.e., report all cases that a non-UI thread touches a UI element). So, would like to know your suggestion.

I entirely agree that UI thread is the thread that creates 'Display'. So, could I understand it in this way: all eclipse UI extensions (e.g., views, dialogs) are created by UI threads? and non-UI threads are spawned from UI thread to handle additional events?

For the confirmed JDT bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=125868

However, seems that through the whole stack trace, there is no "thread.start()" method. so, where does the non-UI thread come from? Did the eclipse framework spawn a non-UI thread for it? (which is not shown in the stack trace?)

Thanks a lot. Any suggestion are highly appreciated. I really would like to understand this problem.

-Sai



Deepak Azad wrote on Mon, 31 October 2011 06:15
UI thread is the thread that creates the 'Display' i.e. the 'main'
thread (the thread which has the name 'main').

Create a breakpoint in the piece of code you want to know runs in UI
thread or not and then try to hit that breakpoint. You will know from
the Debug view if the code is being executed in 'main' thread or not.


Re: How to distinguish UI / non-UI thread in eclipse development [message #754140 is a reply to message #754084] Tue, 01 November 2011 03:20 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 10/31/2011 10:32 PM, Sai wrote:
> More precisely, last stack trace shows:
>
> at org.eclipse.core.internal.jobs.Worker.run(Worker.java:58)
>
>
> Class Worker extends the Thread, and is invoked from anywhere else
> (invoking its run method). I am wondering How could I know which thread
> (and class) creates the Work object, and start the Worker thread?

Theoretically you should be able to analyze the code, e.g. search
references to the Thread/Worker and then figure out from where run() is
invoked. However, this is not going to be easy to implement as a
Thread/Worker object could be created in one place and then started from
another place.
Re: How to distinguish UI / non-UI thread in eclipse development [message #754166 is a reply to message #754140] Tue, 01 November 2011 07:09 Go to previous messageGo to next message
Sai Missing name is currently offline Sai Missing nameFriend
Messages: 25
Registered: March 2011
Junior Member
Thanks Deepak for your quick reply.

Yes, that is exactly what I am going to try, figuring out from where
run() is invoked, and report potential Invalid Thread bugs. We now trying
to use a few static analysis (e.g., call graph, points-to, data-flow) techniques
to fulfill this goal. (hopefully, we can get acceptable results. If so, we
would like to contribute the built tool to the eclipse community, since this
error is so common and hard to prevent).

Following the previous thread, I am still a little bit confused on the following
few points:

1. The Worker class that extends Thread, seems to be the de facto "thread" class in eclipse. To avoid invalid thread access problem, a Worker object that touches UI element should only be invoked by UI-thread, or wrapping UI operations using "Display.asyncExec/syncExec", right? In that sense, why does "where does Thread/Worker objects are created, and been started from another place" matter? As long as the UI operations in Worker/thread are wrapped by "Display.asyncExec/syncExec", that is fine. I do not have extensive experience in hacking eclipse, so can be wrong. Please correct me. Thank you.

2. Following 1., how could I identify code that will be executed in a non-UI thread. Can I assume that the extension points (e.g., a view, a dialog) are invoked by eclipse's UI thread? and other threads that are spawned from the extension points are non-UI thread? Or, is it possible that an extension point can be invoked by a non-thread also? I am not sure on this point.

Thank you very much, really appreciate your help.


Deepak Azad wrote on Mon, 31 October 2011 23:20
On 10/31/2011 10:32 PM, Sai wrote:


Theoretically you should be able to analyze the code, e.g. search
references to the Thread/Worker and then figure out from where run() is
invoked. However, this is not going to be easy to implement as a
Thread/Worker object could be created in one place and then started from
another place.

Re: How to distinguish UI / non-UI thread in eclipse development [message #754362 is a reply to message #754166] Wed, 02 November 2011 10:45 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 11/1/2011 12:39 PM, Sai wrote:

> Following the previous thread, I am still a little bit confused on the
> following
> few points:
>
> 1. The Worker class that extends Thread, seems to be the de facto
> "thread" class in eclipse. To avoid invalid thread access problem, a
> Worker object that touches UI element should only be invoked by
> UI-thread, or wrapping UI operations using "Display.asyncExec/syncExec",
> right? In that sense, why does "where does Thread/Worker objects are
> created, and been started from another place" matter?
I meant that the code analysis will get complicated..
Note that there is also org.eclipse.ui.progress.UIJob.

As long as the UI
> operations in Worker/thread are wrapped by "Display.asyncExec/syncExec",
> that is fine. I do not have extensive experience in hacking eclipse, so
> can be wrong. Please correct me. Thank you.
>
> 2. Following 1., how could I identify code that will be executed in a
> non-UI thread. Can I assume that the extension points (e.g., a view, a
> dialog) are invoked by eclipse's UI thread? and other threads that are
> spawned from the extension points are non-UI thread? Or, is it possible
> that an extension point can be invoked by a non-thread also? I am not
> sure on this point.

I don't know if there are any such 'rules'.
Re: How to distinguish UI / non-UI thread in eclipse development [message #754426 is a reply to message #754362] Wed, 02 November 2011 14:10 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

It varies from component to component. ex, in org.eclipse.ui.* our extensions and services generally rely on operating in the UI thread unless otherwise stated. We'll javadoc any non-UI thread support we have.

PW


Re: How to distinguish UI / non-UI thread in eclipse development [message #754483 is a reply to message #754362] Wed, 02 November 2011 17:30 Go to previous messageGo to next message
Sai Missing name is currently offline Sai Missing nameFriend
Messages: 25
Registered: March 2011
Junior Member
Thanks a lot, Deepak.

As far as I understand, UIJob seems to be a wrapping thread for the Display.asyncExec method. Here is the code I found:

UIJob#run
public final IStatus run(final IProgressMonitor monitor) {
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
Display asyncDisplay = getDisplay();
if (asyncDisplay == null || asyncDisplay.isDisposed()) {
return Status.CANCEL_STATUS;
}
asyncDisplay.asyncExec(new Runnable() {
public void run() {
....
result = runInUIThread(monitor); //overriden by its sub classes
}
}
}

This information is very useful for analysis, for example, the tool can encode (and assume) the runInUIThread method is safe to perform UI operations.


May I ask, like UIJob, are there another commonly-used wrapper class in eclipse framework to perform UI operations?

Thanks a lot

-Sai



Deepak Azad wrote on Wed, 02 November 2011 06:45
On 11/1/2011 12:39 PM, Sai wrote:

> Following the previous thread, I am still a little bit confused on the
> following
> few points:
>
> 1. The Worker class that extends Thread, seems to be the de facto
> "thread" class in eclipse. To avoid invalid thread access problem, a
> Worker object that touches UI element should only be invoked by
> UI-thread, or wrapping UI operations using "Display.asyncExec/syncExec",
> right? In that sense, why does "where does Thread/Worker objects are
> created, and been started from another place" matter?
I meant that the code analysis will get complicated..
Note that there is also org.eclipse.ui.progress.UIJob.

As long as the UI
> operations in Worker/thread are wrapped by "Display.asyncExec/syncExec",
> that is fine. I do not have extensive experience in hacking eclipse, so
> can be wrong. Please correct me. Thank you.
>
> 2. Following 1., how could I identify code that will be executed in a
> non-UI thread. Can I assume that the extension points (e.g., a view, a
> dialog) are invoked by eclipse's UI thread? and other threads that are
> spawned from the extension points are non-UI thread? Or, is it possible
> that an extension point can be invoked by a non-thread also? I am not
> sure on this point.

I don't know if there are any such 'rules'.

Re: How to distinguish UI / non-UI thread in eclipse development [message #754572 is a reply to message #754483] Thu, 03 November 2011 10:44 Go to previous message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 11/2/2011 11:00 PM, Sai wrote:

> May I ask, like UIJob, are there another commonly-used wrapper class in
> eclipse framework to perform UI operations?
I haven't used anything else.
Previous Topic:Error symbol not shown in Navigator view
Next Topic:Handle cycle dependencies using JDT
Goto Forum:
  


Current Time: Sat Apr 27 01:08:30 GMT 2024

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

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

Back to the top