时间:2022-12-06 13:37:24 | 栏目:JAVA代码 | 点击:次
public class ThreadAndAllBreakApplication {
public static void main(String[] args) {
ThreadTest thread1 = new ThreadTest();
thread1.setName("线程A");
thread1.start();
ThreadTest thread2 = new ThreadTest();
thread2.setName("线程B");
thread2.start();
ThreadTest thread3 = new ThreadTest();
thread3.setName("线程C");
thread3.start();
}
}
class ThreadTest extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ": 1");
try {
long millis = RandomUtil.randomLong(100, 500);
System.out.println(Thread.currentThread().getName() + "睡眠: " + millis);
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": 2");
System.out.println(Thread.currentThread().getName() + ": 3");
System.out.println(Thread.currentThread().getName() + ": 设置断点的前一行代码"); // 当前行设置断点
System.out.println(Thread.currentThread().getName() + ": 4");
System.out.println(Thread.currentThread().getName() + ": end");
线程A: 1
线程C: 1
线程B: 1
线程C睡眠: 283
线程A睡眠: 340
线程B睡眠: 127
线程B: 2
线程B: 3
线程B: 设置断点的前一行代码 // B来到了这里,此时其他线程A、B在原地等待,即A、B都在睡眠。
线程A: 2 // A之前执行到睡眠,现在执行2
线程A: 3
线程A: 设置断点的前一行代码 // A来到了这里,此时其他线程B、C在原地等待
线程C: 2 // c之前执行到睡眠,现在执行2
线程C: 3
线程A: 4
线程B: 4
线程A: end
线程C: 设置断点的前一行代码 // C来到了这里,如果不放行断点,B一直也不会输出end,会在原地等待(这里证明了当某个线程被All断点阻塞后,其他线程会在原地等待)
线程B: end
线程C: 4
线程C: end
所有的线程都会运行到断点处然后阻塞
