Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: current thread (which returns from the call to the start method) and the other thread (which executes its run method).
@Test
publicvoidtestThread() throws ConfigException {
Thread t1 = new Thread("t1") {
publicvoidrun() {
for (int i = 0; i < 10000; i++) {
System.out.println(Thread.currentThread() + "t1 run " + i);
}
}
};
Thread t2 = new Thread("t2") {
publicvoidrun() {
try {
for (int i = 0; i < 10000; i++) {
System.out.println(Thread.currentThread() + "t2 run " + i);
Thread.currentThread().sleep(3000L);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
System.out.println(Thread.currentThread() + "begin");
t1.start();
System.out.println(Thread.currentThread() + "t1 start interrupt.");
t1.interrupt();
t2.start();
System.out.println(Thread.currentThread() + "t2 start interrupt.");
t2.interrupt();
}
测试结果:
测试类日志打印 Process finished with exit code 0 Thread[t1,5,main]t1 run 2234
方法类打印日志:Thread[t1,5,main]t1 run 674
t1.start()替换为t1.run()之后,日志:Thread[main,5,main]t1 run 9999 Thread[main,5,main]t1 start interrupt.