🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #10175

Re: Swing, Threads, and Progress meters.

#10175
From: Steve Horsley
Date: Thu, 04 Nov 1999 00:00
54 lines
2942 bytes
By my limited understanding, you have a bit of work on your hands.

There is a concurrency issue here. If your thread changes your model,
and this in turn calls the model watchers which in turn modify the GUI,
the GUI changes are indeed happening in the thread context, which is 
bad news.

I would suggest that you arrange that all the model observers do not
modify the GUI directly, but instead create a Runnable object that will
do the work. You can then use SwingUtilities.invokeLater(Runnable r) to
have the GUI update performed by the GUI event thread.

The Runnable needs to be set up with all the info it needs to do its job
held in instance variables, so when it is called with public void run() 
it knows what to do. You can either tuck a lot of code in the Runnable,
or you could just have it call methods in your GUI class. In the latter
case, the Runnable may just be a one-liner.

Because you can't be sure when a Runnable has finished on another 
thread, you can't safely re-use a Runnable (unless you use
invokeAndWait(Runnable r) instead). You really need to 
create a new one for each GUI update required, throw it at the
event thread, and forget about it.

It's not as hard as it sounds, and I think it's worth the effort 
because then you are free to thread any tasks you like. Your app
will also have a much better "feel" for the users while doing long
tasks as well.

Hope this helps.
Steve.
//--------------------------

"John P. McGrath" wrote:
> 
> On Thu, 04 Nov 1999 06:50:22 GMT, Olaf Raether wrote:
> 
> > take a look at the ProgressMonitor class in javax.swing
> 
> I did - I even mentioned ProgressMonitor in my post.
> 
> > >The ProgressMeter / ProgressMonitor examples I have seen all perform the
> > >long task in a separate thread, and my application currently does all of
> > >its work in the event-dispatch thread.  I am concerned that moving this
> > >work to a separate thread may cause problems.
> 
> My question has to do with the concurrency issues that I think I will
> have to deal with if I use ProgressMeter or ProgressMonitor.  Does
> ProgressMonitor help with this in some way that I am not aware of?
> 
> --
> John P. McGrath
> <mcgrath@enter.net>
> <jpm@nscs.fast.net>
Technical Details
Message-ID: <3821581E.88C37E4C@cwcom.co.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <MPG.1289bc46f1d0f7aa989685@forums.borland.com> <38212c72.1261133@forums.inprise.com> <MPG.128afecc7d95098098968d@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,eebaae9990e98781
X-Google-Attributes: gid111d45,public
From: Steve Horsley <steve.horsley@cwcom.co.uk>
Subject: Re: Swing, Threads, and Progress meters.
Date: 1999/11/04
Message-ID: <3821581E.88C37E4C@cwcom.co.uk>
X-Deja-AN: 544325475
Content-Transfer-Encoding: 7bit
References: <MPG.1289bc46f1d0f7aa989685@forums.borland.com> <38212c72.1261133@forums.inprise.com> <MPG.128afecc7d95098098968d@forums.borland.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: Cable & Wireless Communications
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing