Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Running a Thread multiple times

559 views
Skip to first unread message

A. Farber

unread,
Jul 29, 2004, 9:16:16 AM7/29/04
to
Hi,

I have an applet with a tiled background. On that background
I draw two light spots, which take quite some time to render.

So whenever the applet (actually a Frame in my applet) is
resized I draw the tiles (this doesn't take long) and after
that would like to fire off a background thread which would
calculate the light spots:

class RenderThread extends Thread
{
public volatile boolean running = false;

public RenderThread() {
setPriority(Thread.MIN_PRIORITY);
}

public void start() {
running = true;
super.start();
}

public void run() {
while (running) {
// rendering code
}
}
}

And in my applet/frame code I call:

void init() {
thread = new RenderThread();
}

public void resize(int w, int h) {
if (thread.isAlive()) {
thread.running = false;
try {
thread.join();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// draw the tiles here (that is quick)
super.setSize(w, h);
thread.start();
}

Unfortunately this doesn't work as expected - the thread
runs only once. I'm sorry that I don't have a handy test
case to show, but could anyone please answer a general
question for me:

Can you instantiate a Thread object and then start()
and halt it (by returning from run()) several times?

Thanks
Alex

SPG

unread,
Jul 30, 2004, 12:20:22 PM7/30/04
to
Why don't you just create a new instance of the thread then run it when
needed?

public void resize(int w, int h) {
if (thread.isAlive()) {
thread.running = false;
try {
thread.join();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// draw the tiles here (that is quick)
super.setSize(w, h);

thread = new RenderThread();
thread.start();
}

This will always wait for the existing threa to stop then start a new one..

Steve

"A. Farber" <Alexande...@t-online.de> wrote in message
news:c9ccaf83.04072...@posting.google.com...

0 new messages