public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new MyThread();
t.start();
Thread.sleep(1000);
t.interrupt(); // 中断t线程
t.join(); // 等待t线程结束
System.out.println("end");
}
class MyThread extends Thread {
public void run() {
Thread hello = new HelloThread();
hello.start(); // 启动hello线程
try {
hello.join(); // 等待hello线程结束
} catch (InterruptedException e) {
System.out.println("interrupted!");
hello.interrupt();
class HelloThread extends Thread {
int n = 0;
while (!isInterrupted()) {
n++;
System.out.println(n + " hello!");
Thread.sleep(100);
break;
分析一下代码:
1、main() 主线程,启动。
2、Thread t = new MyThread();
t 线程启动。
现在有2个线程 在运行了。
3、Thread.sleep(1000);
主线程 睡一秒钟,太好了,这1秒中之内,主线程不会干扰t线程的运行,我们去看看t线程他在做什么。
4、Thread hello = new HelloThread();
原来t线程,启动了。一个hello线程。
现在有3条线程了。主线程还在睡觉,暂时不用管它,1秒钟可真长。
5、hello.join(); // 等待hello线程结束
hello 线程强行,进入,t线程只能乖乖的去等待hello去执行任务。此时,主线程依然在睡觉,1秒钟可真长。。
我们去看看hello 线程在做什么。
6、run() 方法...
原来 hello 线程,写了一个循环,每隔0.1秒,打印一个hello。
hello
...
打印,hello中。
完了,这好像,是一个死循环啊。怎么办,怎么办。
当打印到第10个hello的时候,我们主线程终于睡醒了。
7、 t.interrupt(); // 中断t线程
好家伙,一睡醒,就不干好事。上来就去中断,t线程,我们去看看t线程能不能被中断。虽然主线程不干好事,但最终的决定权在t线程手上。 坏菜了,我们忘记了。这个t线程。还在等待hello线程执行任务。根据廖老师说的,t线程受不了了,不仅受hello线程欺负(强行加入),又主线程欺负(中断),t线程不干了。撂挑子了,抛异常 InterruptedException , 不过,幸运的是,我提前预判了,t线程会撂挑子,并且成功捕获了异常,所以,t线程只是发了下恼骚,打印 interrupted之后, 继续干活。
8、 hello.interrupt();
因为有join方法的存在,t线程继续干活,这次,他也硬气了一点,去中断hello线程。巧了么,不是,虽然hello线程只睡0.1秒,但是,刚好,t线程中断的时候,hello线程在睡觉。hello线程也不干了。撂挑子了,抛异常 InterruptedException ,好在我们又一次未卜先知,捕获到了异常。我们真是太厉害了。
9、 break;
hello 线程任务,结束。t线程任务结束。回归主线程,打印end结束。
整个流程结束。
至于为什么要break,评论区找答案吧。
Sign in to make a reply
小丑竟是我自己
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new MyThread();
t.start();
Thread.sleep(1000);
t.interrupt(); // 中断t线程
t.join(); // 等待t线程结束
System.out.println("end");
}
}
class MyThread extends Thread {
public void run() {
Thread hello = new HelloThread();
hello.start(); // 启动hello线程
try {
hello.join(); // 等待hello线程结束
} catch (InterruptedException e) {
System.out.println("interrupted!");
}
hello.interrupt();
}
}
class HelloThread extends Thread {
public void run() {
int n = 0;
while (!isInterrupted()) {
n++;
System.out.println(n + " hello!");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
}
分析一下代码:
1、main() 主线程,启动。
2、Thread t = new MyThread();
t.start();
t 线程启动。
现在有2个线程 在运行了。
3、Thread.sleep(1000);
主线程 睡一秒钟,太好了,这1秒中之内,主线程不会干扰t线程的运行,我们去看看t线程他在做什么。
4、Thread hello = new HelloThread();
hello.start(); // 启动hello线程
原来t线程,启动了。一个hello线程。
现在有3条线程了。主线程还在睡觉,暂时不用管它,1秒钟可真长。
5、hello.join(); // 等待hello线程结束
hello 线程强行,进入,t线程只能乖乖的去等待hello去执行任务。此时,主线程依然在睡觉,1秒钟可真长。。
我们去看看hello 线程在做什么。
6、run() 方法...
原来 hello 线程,写了一个循环,每隔0.1秒,打印一个hello。
hello
hello
hello
...
打印,hello中。
完了,这好像,是一个死循环啊。怎么办,怎么办。
hello
hello
hello
...
打印,hello中。
当打印到第10个hello的时候,我们主线程终于睡醒了。
7、 t.interrupt(); // 中断t线程
t.join(); // 等待t线程结束
好家伙,一睡醒,就不干好事。上来就去中断,t线程,我们去看看t线程能不能被中断。虽然主线程不干好事,但最终的决定权在t线程手上。
坏菜了,我们忘记了。这个t线程。还在等待hello线程执行任务。根据廖老师说的,t线程受不了了,不仅受hello线程欺负(强行加入),又主线程欺负(中断),t线程不干了。撂挑子了,抛异常 InterruptedException , 不过,幸运的是,我提前预判了,t线程会撂挑子,并且成功捕获了异常,所以,t线程只是发了下恼骚,打印 interrupted之后, 继续干活。
8、 hello.interrupt();
因为有join方法的存在,t线程继续干活,这次,他也硬气了一点,去中断hello线程。巧了么,不是,虽然hello线程只睡0.1秒,但是,刚好,t线程中断的时候,hello线程在睡觉。hello线程也不干了。撂挑子了,抛异常 InterruptedException ,好在我们又一次未卜先知,捕获到了异常。我们真是太厉害了。
9、 break;
hello 线程任务,结束。t线程任务结束。回归主线程,打印end结束。
整个流程结束。
至于为什么要break,评论区找答案吧。