Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » about swt 's thread .
about swt 's thread . [message #451611] Thu, 03 March 2005 11:33 Go to next message
bbskill is currently offline bbskillFriend
Messages: 26
Registered: July 2009
Junior Member
if a long run Thread update a widget many times,then which
code is better?
first:
updateRunnable = new Runnable() {
public void run() {
//update a widget;
}
}
new Thread() {
public void run() {
for(int i = 0 ; i< manytimes; i++) {
//compute the data ,costs some time
display.syncExec(updateRunnable);
//compute some data,costs some time
}
}
}.start();

second:
display.asyncExec(new Runnable() {
public void run() {
for(int i = 0 ; i< manytimes; i++) {
//compute the data ,costs some time
//update a widget;
//compute some data,costs some time
}
}
});
Thank you for your help !
Re: about swt 's thread . [message #451616 is a reply to message #451611] Thu, 03 March 2005 12:22 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
The first way is better.

The second way still runs in the UI thread and if the data computation takes
a minute or so, you will end up blocking the UI thread and the application
will be unresponsive (hung) until it completes. Display.asyncExec runs when
the event loop is idle but it may not be idle for very long.


"bbskill" <bbkills@tom.com> wrote in message
news:d06sqn$d69$1@www.eclipse.org...
> if a long run Thread update a widget many times,then which
> code is better?
> first: updateRunnable = new Runnable() {
> public void run() {
> //update a widget; }
> }
> new Thread() {
> public void run() {
> for(int i = 0 ; i< manytimes; i++) {
> //compute the data ,costs some time
> display.syncExec(updateRunnable);
> //compute some data,costs some time
> }
> }
> }.start();
>
> second:
> display.asyncExec(new Runnable() {
> public void run() {
> for(int i = 0 ; i< manytimes; i++) {
> //compute the data ,costs some time
> //update a widget; //compute some data,costs some time
> }
> }
> });
> Thank you for your help !
Re: about swt 's thread . [message #451620 is a reply to message #451616] Thu, 03 March 2005 12:32 Go to previous messageGo to next message
bbskill is currently offline bbskillFriend
Messages: 26
Registered: July 2009
Junior Member
Thanke you for your advise.

Veronika Irvine wrote:
> The first way is better.
>
> The second way still runs in the UI thread and if the data computation takes
> a minute or so, you will end up blocking the UI thread and the application
> will be unresponsive (hung) until it completes. Display.asyncExec runs when
> the event loop is idle but it may not be idle for very long.
>
>
> "bbskill" <bbkills@tom.com> wrote in message
> news:d06sqn$d69$1@www.eclipse.org...
>
>>if a long run Thread update a widget many times,then which
>>code is better?
>>first: updateRunnable = new Runnable() {
>>public void run() {
>>//update a widget; }
>>}
>>new Thread() {
>>public void run() {
>>for(int i = 0 ; i< manytimes; i++) {
>>//compute the data ,costs some time
>>display.syncExec(updateRunnable);
>>//compute some data,costs some time
>>}
>>}
>>}.start();
>>
>>second:
>>display.asyncExec(new Runnable() {
>>public void run() {
>>for(int i = 0 ; i< manytimes; i++) {
>>//compute the data ,costs some time
>>//update a widget; //compute some data,costs some time
>>}
>>}
>>});
>>Thank you for your help !
>
>
>
Re: about swt 's thread . [message #451636 is a reply to message #451616] Thu, 03 March 2005 19:02 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: bob.objfac.com

Essentially you're saying the two ways are equivalent and the first way
is simpler. True, but if computing the data took a minute or more,
wouldn't he be better off running the compute in a separate thread in
the background, then using asyncExec to update the widget? A minute is
an awfully long time to hang the UI thread.

Bob

Veronika Irvine wrote:
> The first way is better.
>
> The second way still runs in the UI thread and if the data computation takes
> a minute or so, you will end up blocking the UI thread and the application
> will be unresponsive (hung) until it completes. Display.asyncExec runs when
> the event loop is idle but it may not be idle for very long.
>
>
> "bbskill" <bbkills@tom.com> wrote in message
> news:d06sqn$d69$1@www.eclipse.org...
>
>>if a long run Thread update a widget many times,then which
>>code is better?
>>first: updateRunnable = new Runnable() {
>>public void run() {
>>//update a widget; }
>>}
>>new Thread() {
>>public void run() {
>>for(int i = 0 ; i< manytimes; i++) {
>>//compute the data ,costs some time
>>display.syncExec(updateRunnable);
>>//compute some data,costs some time
>>}
>>}
>>}.start();
>>
>>second:
>>display.asyncExec(new Runnable() {
>>public void run() {
>>for(int i = 0 ; i< manytimes; i++) {
>>//compute the data ,costs some time
>>//update a widget; //compute some data,costs some time
>>}
>>}
>>});
>>Thank you for your help !
>
>
>
Re: about swt 's thread . [message #451639 is a reply to message #451636] Thu, 03 March 2005 19:29 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Maybe my words were not very clear. The two ways are not the same at all.
The second one hangs up the UI thread if it runs for any significant length
of time. The second way starts the work when the event loop is idle but
will block new events until it completes and therefore is bad. The first
way executes only the minimum amount of work in the UI thread (updating the
UI) and therefore does not or hang the GUI - that is why it is better.

"Bob Foster" <bob@objfac.com> wrote in message
news:42275F3D.2090705@objfac.com...
> Essentially you're saying the two ways are equivalent and the first way is
> simpler. True, but if computing the data took a minute or more, wouldn't
> he be better off running the compute in a separate thread in the
> background, then using asyncExec to update the widget? A minute is an
> awfully long time to hang the UI thread.
>
> Bob
>
> Veronika Irvine wrote:
>> The first way is better.
>>
>> The second way still runs in the UI thread and if the data computation
>> takes a minute or so, you will end up blocking the UI thread and the
>> application will be unresponsive (hung) until it completes.
>> Display.asyncExec runs when the event loop is idle but it may not be idle
>> for very long.
>>
>>
>> "bbskill" <bbkills@tom.com> wrote in message
>> news:d06sqn$d69$1@www.eclipse.org...
>>
>>>if a long run Thread update a widget many times,then which
>>>code is better?
>>>first: updateRunnable = new Runnable() {
>>>public void run() {
>>>//update a widget; }
>>>}
>>>new Thread() {
>>>public void run() {
>>>for(int i = 0 ; i< manytimes; i++) {
>>>//compute the data ,costs some time
>>>display.syncExec(updateRunnable);
>>>//compute some data,costs some time
>>>}
>>>}
>>>}.start();
>>>
>>>second:
>>>display.asyncExec(new Runnable() {
>>>public void run() {
>>>for(int i = 0 ; i< manytimes; i++) {
>>>//compute the data ,costs some time
>>>//update a widget; //compute some data,costs some time
>>>}
>>>}
>>>});
>>>Thank you for your help !
>>
>>
Re: about swt 's thread . [message #451649 is a reply to message #451639] Fri, 04 March 2005 06:09 Go to previous message
Eclipse UserFriend
Originally posted by: bob.objfac.com

My humble apologies. I misread the first example. It's exactly what I
suggested. Feeling pretty foolish, but thanks for your kind reply.

Bob

Veronika Irvine wrote:
> Maybe my words were not very clear. The two ways are not the same at all.
> The second one hangs up the UI thread if it runs for any significant length
> of time. The second way starts the work when the event loop is idle but
> will block new events until it completes and therefore is bad. The first
> way executes only the minimum amount of work in the UI thread (updating the
> UI) and therefore does not or hang the GUI - that is why it is better.
>
> "Bob Foster" <bob@objfac.com> wrote in message
> news:42275F3D.2090705@objfac.com...
>
>>Essentially you're saying the two ways are equivalent and the first way is
>>simpler. True, but if computing the data took a minute or more, wouldn't
>>he be better off running the compute in a separate thread in the
>>background, then using asyncExec to update the widget? A minute is an
>>awfully long time to hang the UI thread.
>>
>>Bob
>>
>>Veronika Irvine wrote:
>>
>>>The first way is better.
>>>
>>>The second way still runs in the UI thread and if the data computation
>>>takes a minute or so, you will end up blocking the UI thread and the
>>>application will be unresponsive (hung) until it completes.
>>>Display.asyncExec runs when the event loop is idle but it may not be idle
>>>for very long.
>>>
>>>
>>>"bbskill" <bbkills@tom.com> wrote in message
>>>news:d06sqn$d69$1@www.eclipse.org...
>>>
>>>
>>>>if a long run Thread update a widget many times,then which
>>>>code is better?
>>>>first: updateRunnable = new Runnable() {
>>>>public void run() {
>>>>//update a widget; }
>>>>}
>>>>new Thread() {
>>>>public void run() {
>>>>for(int i = 0 ; i< manytimes; i++) {
>>>>//compute the data ,costs some time
>>>>display.syncExec(updateRunnable);
>>>>//compute some data,costs some time
>>>>}
>>>>}
>>>>}.start();
>>>>
>>>>second:
>>>>display.asyncExec(new Runnable() {
>>>>public void run() {
>>>>for(int i = 0 ; i< manytimes; i++) {
>>>>//compute the data ,costs some time
>>>>//update a widget; //compute some data,costs some time
>>>>}
>>>>}
>>>>});
>>>>Thank you for your help !
>>>
>>>
>
Previous Topic:Is there a way to actively "read" keyboard modifier state?
Next Topic:TableViewer behaviour with SWT.VIRTUAL in 3.1-M5a?
Goto Forum:
  


Current Time: Tue Sep 29 21:15:50 GMT 2020

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

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

Back to the top