java中如何靠着throw抛出一个异常来停止线程
把上面的程序return,变成自己通过throw主动抛出异常,结果是一样的。
例:1.5.1_1-本章源码
class mythreadmark_to_win extends thread{
private boolean stop;
public void run() {
for (int i = 0; i < 100; i++) {
if (stop) {
system.out.println("退出了");
throw new arithmeticexception("divide by 0");
}
try {
thread.sleep(200);
} catch (exception e) {
e.printstacktrace();
}
system.out.println("i = " + i);
}
}
public void setstop(boolean stop) {
this.stop = stop;
}
}
public class test {
public static void main(string[] args) {
mythreadmark_to_win mt = new mythreadmark_to_win();
mt.start();
try {
。。。。。。。。。。。。。。。。。
详情请进:http://www.mark-to-win.com/index.html?content=javabeginner/javaurl.html&chapter=javabeginner/javabeginner6_web.html#stopthreadbythread
下一篇: 网站运营不成功的原因