Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2005

Tip: Looking for answers? Try searching our database.

Implementing my own Eventloop

Thread view: 
christian mader - 04 Jul 2005 22:31 GMT
Hi everyone!

I have to port a MFC-based frontend for an old terminal-based
application to a more modern language/toolkit. So far I've managed it to
run under Linux/GTK+ and I'm now evaluating if it can also be implemented
using java. It all boils down to one question: The GUI is kept busy due to
a loop in the application's code. Is it possible to start an second event
loop (besides the one started by the VM making AWT work) so that the GUI
is responsive again?
Here are some further details: My application should react on a remote
procedure call, then let the user do some input in a form and return from
the called procedure. It's mainly mapping an old app that works with an
internal eventloop and callback functions to AWT's event-driven
philosophy. In GTK+ I simply start gtk_main() when the GUI should be
responsive and do gtk_main_quit() when the user hits esc and the remotely
called procedure can return. Is there a similar way I can do this in java?

thanx for your help!
christian
Chris Smith - 05 Jul 2005 00:14 GMT
> I have to port a MFC-based frontend for an old terminal-based
> application to a more modern language/toolkit. So far I've managed it to
[quoted text clipped - 3 lines]
> loop (besides the one started by the VM making AWT work) so that the GUI
> is responsive again?

No.  You should delegate long-running tasks to worker threads, allowing
the main thread to get back to the task of processing the event loop.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Thomas Weidenfeller - 05 Jul 2005 07:44 GMT
> using java. It all boils down to one question: The GUI is kept busy due to
> a loop in the application's code.

If the loop does "busy waiting", read, just burns CPU cycles while
waiting for something to happen, it is bad anyhow on a multi-tasking OS.
If this is the case you need to change that regardless of language.

> Is it possible to start an second event
> loop (besides the one started by the VM making AWT work) so that the GUI
> is responsive again?

No, start a thread. See Q3.1 and Q3.2 of the FAQ.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Tjerk Wolterink - 05 Jul 2005 10:47 GMT
>> using java. It all boils down to one question: The GUI is kept busy
>> due to
[quoted text clipped - 3 lines]
> waiting for something to happen, it is bad anyhow on a multi-tasking OS.
> If this is the case you need to change that regardless of language.

Isnt that how all event based programs ar built?
There must be a busy waiting thread to constantly check
for user events.
Thomas Weidenfeller - 05 Jul 2005 11:11 GMT
> Isnt that how all event based programs ar built?
> There must be a busy waiting thread to constantly check
> for user events.

No, not at all. "busy waiting" means you use the CPU to constantly check
for a condition in a tight loop. Which is in general very bad:

    while(numQueuedEvents == 0) {
        ;    // burn CPU cycles
    }

A well written event-driven program releases the thread (the CPU) when
there is nothing to do. And the thread gets woken up once an event
arrives. It does/should check an exit condition in regular intervals if
the thread is supposed to be terminated prematurely. But that shouldn't
happen in a tight loop at all

Pseudo code (ignoring thread safety, exceptions, and many details):

    public void run() {
        while(!exitQueue) {
            if(numQueuedEvents == 0) {
                wait();    // give up CPU
            }
            Event e = getEventFromQueue();
            numQueuedEvents--;
            ...
            ...
        }
    }

    public void queueEvent(Event e) {
        addToQueue(e);
        numQueuedEvents++;
        enventHander.notify(); // wake up queue processing
    }

In lower-level terms we are talking about polling for some condition vs.
waiting for an interrupt informing about a condition.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Thomas Hawtin - 14 Jul 2005 22:12 GMT
> I have to port a MFC-based frontend for an old terminal-based
> application to a more modern language/toolkit. So far I've managed it to
[quoted text clipped - 10 lines]
> responsive and do gtk_main_quit() when the user hits esc and the remotely
> called procedure can return. Is there a similar way I can do this in java?

Contrary to other reports I believe it is possible.

As I understand it: There is a hack for dialogue boxes to run an event
loop. Some API allow an application developer to open a dialogue box
from the Event Dispatch Thread (EDT) and wait for it to be closed. Some
magic going on there obviously. IIRC, Foxtrot (google it) exploits
dialogues to give a nice interface to this functionality.

Having said that, it is very much cleaner to use separate threads. Just
be very, very careful.

Tom Hawtin
Signature

Unemployed English Java programmer

Thomas Weidenfeller - 15 Jul 2005 09:31 GMT
> Contrary to other reports I believe it is possible.
>
> As I understand it: There is a hack for dialogue boxes to run an event
> loop. Some API allow an application developer to open a dialogue box
> from the Event Dispatch Thread (EDT) and wait for it to be closed. Some
> magic going on there obviously.

It is not magic. It is just that the event pump is replaced, so only
events for the modal dialog are pumped. There are maybe 5 to ten lines
of code involved in the Dialog source code to make the dialog modal.

> IIRC, Foxtrot (google it) exploits
> dialogues to give a nice interface to this functionality.
>
> Having said that, it is very much cleaner to use separate threads. Just
> be very, very careful.

I find it rather hilarious that Foxtrot claims to replace Threads by
using - well you guessed it - a worker Thread.

I still prefer the classic way:

    public void actionPerformed(ActionEvent e) {
        new Thread(new Runnable() {
            public void run() {
                //
                // Do some time consuming task
                //
                ...

                //
                // Update the GUI from within the task
                // synchronous: invokeAndWait()
                // asynchronous: invokeLater()
                //
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        //
                        // GUI code
                        //
                    }
                });

            }
        }).start();
    }

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Thomas Hawtin - 15 Jul 2005 15:12 GMT
> It is not magic. It is just that the event pump is replaced, so only
> events for the modal dialog are pumped. There are maybe 5 to ten lines
> of code involved in the Dialog source code to make the dialog modal.

Looking at the source of java.awt.Dialog.show, I'd call that magic. It's
huge and it uses non-public interfaces.

Technically there are events other than for the modal dialog that are
let through. For instance repaints.

> I find it rather hilarious that Foxtrot claims to replace Threads by
> using - well you guessed it - a worker Thread.

"Foxtrot is a small but powerful framework for using threads with the
JavaTM Foundation Classes (JFC/Swing)."
  -- http://foxtrot.sourceforge.net/docs/introduction.php

> I still prefer the classic way:

Absolutely. Like that or with queues or similar. I tend to have the
non-EDT task and the invokeLater task separate.

What I really don't like about the Foxtrot approach is that the causing
event hasn't finished executing (obviously). So now you have problems of
listeners that haven't fired and of re-entrancy.

Tom Hawtin
Signature

Unemployed English Java programmer



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.