On GUI Multithreading

So everybody seems nervous about doing multithreading on GUIs. It seems some people thinks Swing should be thread safe.

That's nonsense! There's not a single GUI in the market (AFAIK) that is thread safe!. And there's a simple reason for that: it's not possible or desireable to build one!

In fact Microsoft's MFC, GTK, wxWidgets (aka wxWindows), SWT and QT are not threadsafe. (and you could include Motif 2.0, Athena, Xt and some other older toolkits here)

All these toolkits use a thread to dispatch events to widgets (the event-loop thread) and, maybe, some others for paiting. Same thing in Swing.

So if you perform heavy-duty, time-consuming, stuff in the event loop thread you pause sending events (and/or repainting) and resume later after the time-consuming stuff ends. Meanwhile your GUI seems to hang, does not respond (because no events are being processed) and, depending on the toolkit, may even stop painting the widgets.

It's that simple. And it has to be. There's no workaround. You have to live with it.

Now, if you investigate a little bit on how MFC, GTK, wxWidgets, SWT or any other ease this task you'll probably find out that the new SwingWorker is just a piece of cake.

And, with the addition ( JSR 166) of the excellent Doug Lea's concurrent utilities to JDK 5.0, the rebranded SwingWorker offers new possibilities to boost your GUI performance and your productivity.

I'm for holidays today, too weeks around Spain, but I promise posting some sample code of SwingWorker by the end of the month (and maybe some photos too).

Meanwhile I'll accept suggestions for examples. Please include a comment below about what sort of thing you would like me to work on the example. Should be simple enough for a short example. Hints: invoke an EJB on the background? invoke a web service? retrieve content from a RSS feed? a JTree for a filesystem that seeks the nodes (files) in the background?

Happy Swinging!!

Comentarios:

my favorite is: execute a database query(or some thing heavy) in side a button's actionListener, throw a modal dialog to user, to block user from interacting with ui. (1) should be able to specify no of milliseconds to wait. i.e, if it is specified 750 msec, then if the heavy operation is completed within 750 msec, then modal dialog should never popup. In other words, the modal dialog should popup if and only if the heavy operation couldn't be completed in 750 msec. (2) the modal dialog should have a [cancel] button. the cancel should be more matured. let us say, inside the heavy operation i am debiting an amount from a bank-databse and crediting in another database. on pressing [cancel], the heavy thread should get a chance to rollback the transactions.

Enviado por Santhosh Kumar en julio 05, 2005 a las 11:16 AM CEST #

sdzfdfs

Enviado por guest en julio 06, 2005 a las 08:47 AM CEST #

Enviar un comentario:
Los comentarios han sido deshabilitados.
About

swinger

Search

Archives
« febrero 2015
lunmarmiéjueviesábdom
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
       
Hoy