|
Control a thread from outside |
|
|
This tips shows a structure for threads which allows you to control execution of them outside easily.
public class TT extends Thread {
static final int RUN = 0;
static final int SUSPEND = 1;
static final int STOP = 2;
private int state = RUN;
public synchronized void setState(int s) {
state = s;
if (s == RUN) notify();
}
private boolean boolean checkState() {
while (state == SUSPEND) {
try {
wait();
}
catch (Exception e) {}
}
if (state == STOP)
return false;
return true;
}
public void run() {
while true {
doSomething();
if (!checkState())
break;
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.