这一章节我们来讨论一下另一种停止线程的方法-return
1.在主线程上面return,是把所有在运行的线程都停掉
package com.ray.deepintothread.ch01.topic_9;
public class StopByReturn {
public static void main(String[] args) throws InterruptedException {
ThreadFive threadFive = new ThreadFive();
Thread thread1 = new Thread(threadFive);
thread1.start();
Thread thread2 = new Thread(threadFive);
thread2.start();
Thread.sleep(200);
return;
}
}
class ThreadFive implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " begin");
try {
System.out.println(Thread.currentThread().getName() + " working");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " exit");
}
}
}
输出:
Thread-1 begin
Thread-1 working
Thread-0 begin
Thread-0 working<