62,616
社区成员
发帖
与我相关
我的任务
分享
public class ThreadTest {
public static void main(String[] args) {
try {
System.out.println("try");
Thread thread = new MyThread();
thread.start();
thread.interrupt();
} catch (Exception e) {
System.out.println("1exception");
} finally {
System.out.println("finally");
}
}
}
class MyThread extends Thread {
public void run() {
try {
System.out.println("run");
for(int i=0;i<10000;i++)
System.out.print(i);
System.out.println();
Thread.sleep(1000);
throw new Exception();
} catch (Exception e) {
System.out.println("2exception ");
}
}
}
try
finally
run
0123456789
2exception
public class Test4 {
public static void main(String[] args) throws Exception{
TestThread t = new TestThread();
t.start();
Thread.sleep(3000);// 睡3秒就唤醒线程
t.interrupt();
}
}
class TestThread extends Thread{
@Override
public void run() {
for(int i = 0; i < 5; i ++){
try {
Thread.sleep(1000);
System.out.println(i);
} catch (InterruptedException e) {
break;
}
}
System.out.println("TestThread goon...");
}
}
public class ThreadTest {
public static void main(String[] args) {
try {
System.out.println("try");
Thread thread = new MyThread();
thread.start();
thread.interrupt();
} catch (Exception e) {
System.out.println("1exception");
} finally {
System.out.println("finally");
}
}
}
class MyThread extends Thread {
public void run() {
try {
System.out.println("run");
Thread.sleep(100000); //多加了这行
System.out.println("addition");
throw new Exception();
} catch (Exception e) {
System.out.println("2exception ");
}
}
}
输出:
try
finally
run
2exception
这个到底有什么不同?
可以解释下着两个程度的执行过程么?