多线程程序的特点
- 线程是异步执行的,每次的执行顺序是不固定的,所以程序的执行结果也是不可复现的。
- 通过线程调试可以让每个线程按照调试人员想要的顺序执行,方便调试者调试可疑的程序代码。
写一段多线程的程序
public class MultiThreadExample {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyRunnable("Thread 1"));
Thread thread2 = new Thread(new MyRunnable("Thread 2"));
Thread thread3 = new Thread(new MyRunnable("Thread 3"));
thread1.start();
thread2.start();
thread3.start();
}
static class MyRunnable implements Runnable {
private String name;
public MyRunnable(String name) {
this.name = name;
}
@Override
public void run() {
try {
for (int i = 0; i < 5; i++) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
设置断点

调试断点
