Reputation: 209
If run() hasen't finished and is recalled from outside I need to finish the running shread first.
public class EnvTime extends Thread {
public void run() {
long step = 2000 / benvelope1.length;
while (!finished) {
for (int i = 0; i < benvelope1.length; i++) {
envOut = benvelope1[i];
try {
Thread.sleep(step);
} catch (InterruptedException benvelope1) {
benvelope1.printStackTrace();
}
}
}
}
}
So I call this code from another method with:
Env interpol;
interpol.interrupt(); //
interpol=new EnvTime();
interpol.start();
But this is not working...
Upvotes: 0
Views: 148
Reputation: 8466
You should change finished
somewhere in your code.
Like this way:
try {
Thread.sleep(step);
} catch (InterruptedException benvelope1) {
finished = true;
benvelope1.printStackTrace();
}
Maybe you should also wait that the thread is really terminated before starting the new one. Use join:
interpol.interrupt();
interpol.join();
interpol=new EnvTime();
interpol.start();
Upvotes: 0
Reputation: 420991
It's not clear what you're trying to achieve, but by doing
Env interpol;
interpol.interrupt(); //
you'd probably get a NullPointerException
. If you want your code to reach the
} catch (InterruptedException benvelope1) {
...
}
you need to make sure the thread is in the try
-block, specifically in the Thread.sleep
method when you interrupt it.
In other words, you at least need to start the thread before interrupting it.
Upvotes: 1