读书人

线程Interrupt基础有关问题 得分好贴

发布时间: 2012-02-14 19:19:19 作者: rapoo

线程Interrupt基础问题 得分好贴
public class ShowStatusThread implements Runnable {

Thread showStatusThread = null;

Label label = null;

GC gc = null;

boolean start = true;
boolean wait = false;

public ShowStatusThread(Label label, GC gc, int thread_priority, String name) {
showStatusThread = new Thread(this);
showStatusThread.setPriority(thread_priority);
showStatusThread.setName(name);
this.label = label;
this.gc = gc;
}

public void run() {
while (start) {
if (wait == true) {
synchronized (showStatusThread) {
try {
showStatusThread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
ReadCardView.paintWaitingPic(label, gc);
try {
showStatusThread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public void start() {
if (this.showStatusThread != null) {
this.showStatusThread.start();
}
}

public void stop() {
State s = showStatusThread.getState();
if (State.WAITING.equals(s)) {
showStatusThread.interrupt();
System.out.println(showStatusThread.getState());

} else {
this.start = false;
}
}

public void notify_self() {
synchronized (showStatusThread) {
showStatusThread.notifyAll();
System.out.println(showStatusThread.getPriority());
this.wait = false;
}
}

public void wait_self() {
this.wait = true;
}

public String getName() {
return this.showStatusThread.getName();
}
}


如上代码 线程启动后
我首先调用wait_self() 方法 让线程进入WAIT状态
然后再调用stop() 方法 想让它停止
结果抛出了:
java.lang.InterruptedException

求教怎样解决?

[解决办法]
当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常

读书人网 >J2EE开发

热点推荐