AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Spawning A Thread in a Listener

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
Guy Sussman
Guest





PostPosted: Sun Sep 28, 2003 4:16 pm    Post subject: Spawning A Thread in a Listener Reply with quote



I am spawning a thread within a button listener and find that code
after the invocation of Thread.start() will run before the
Thread.run() method returns. The code resembles the pseudo code
below. Thanks in advance:

_______________________


class CollectandStoreDataThread extends Thread
{
public void run(){
...
...
//collect someData
HoldData.setState(someData);
}
}


class HoldData
{
private static int state;
...
...
public static void setState(data){state = data};
public static int getState(){return state;}
}


class AButtonListener extends ActionListener
{
public void actionPerformed(actionEvent e){
Thread thread = new CollectandStoreDataThread();
thread.start();

int currentState = HoldData.getState();
if(currentState == /*some condition*/){
//do something
}
}
Back to top
SPC
Guest





PostPosted: Mon Sep 29, 2003 11:04 am    Post subject: Re: Spawning A Thread in a Listener Reply with quote



Well, that's what it's supposed to do. The call to start() returns the
moment the thread has started. If you want to hang around til the
thread completes you have to join() it. If you want to wait for the
thread to complete, what's to stop you from just running the code
directly in your button listener?

Usually the behaviour you see is what's wanted. Spin off something
longer term that won't block the event thread, and have it tell you
it's completed by using SwingUtilities.invokeLater. Go to the Sun site
and take a look at SwingWorker, which has most of that sort of thing
packaged up.

Steve
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.