java中如何靠着throw抛出一个异常来停止线程
来源:互联网 发布:艾滋病 知乎 编辑:程序博客网 时间:2024/06/02 00:31
把上面的程序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();
。。。。。。。。。。。。。。。。。
详情请进:http://www.mark-to-win.com/JavaBeginner/JavaBeginner6_web.html#StopThreadByThread
例: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();
。。。。。。。。。。。。。。。。。
详情请进:http://www.mark-to-win.com/JavaBeginner/JavaBeginner6_web.html#StopThreadByThread
0 0
- java中如何靠着throw抛出一个异常来停止线程
- java中如何靠interrupt来停止stop一个线程
- java throw抛出异常
- java中异常的抛出:throw throws
- Java中throws,throw抛出异常问题
- java线程中try catch finally块和throw抛出异常
- java中一个子线程如何通过interrupt手段来停止另外一个子线程
- c# 用 throw 来抛出异常
- Java中抛出异常try...catch和throw简易区别
- JAVA中异常抛出、捕获机制—try catch throw
- java中一个子线程如何通过interrupt手段来停止主线程
- 如何停止一个Java线程
- [Java] throw Exception 声名抛出异常
- 如何Mysql触发器中抛出一个异常
- java中的throw new Exception();和throw e;异常抛出
- 1.7.3停止线程(抛出异常)
- java中如何停止线程
- java中如何停止(stop)一个线程
- 关于Ubuntu下 adb 出现no permissions问题
- hdu 1689 Just a Hook
- session 存入 memcahce
- Java NIO学习资源收集
- HTML5 文本属性
- java中如何靠着throw抛出一个异常来停止线程
- 欢迎使用CSDN-markdown编辑器
- linux exercise 6
- TCP/IP 协议栈 ------ ICMP
- 理解OAuth 2.0
- 类似于五子棋的三子棋小游戏
- (LeetCode) 108. Convert Sorted Array to Binary Search Tree
- LaTex用法
- java中如何靠interrupt来停止stop一个线程