 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guy Sussman Guest
|
Posted: Sun Sep 28, 2003 4:16 pm Post subject: Spawning A Thread in a Listener |
|
|
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
|
Posted: Mon Sep 29, 2003 11:04 am Post subject: Re: Spawning A Thread in a Listener |
|
|
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 |
|
 |
|
|
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
|
|