二、多线程之Thread中run 和start 区别

Thread使用run 和start 区别

结论:run()方法将作为当前调用线程本身的常规方法调用执行,并且不会发生多线程。

System.out.println("开始测试多线程");
Thread thread1= new Thread() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("===Threading count:" + i);
}
}
};
thread1.run();
System.out.println("结束测试多线程");

开始测试多线程
===Threading count:0
===Threading count:1
===Threading count:2
===Threading count:3
===Threading count:4
结束测试多线程

System.out.println("开始测试多线程");
Thread thread1= new Thread() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("===Threading count:" + i);
}
}
};
thread1.start();
System.out.println("结束测试多线程");

开始测试多线程
结束测试多线程
===Threading count:0
===Threading count:1
===Threading count:2
===Threading count:3
===Threading count:4

System.out.println("开始测试多线程");
new Runnable(){
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("===Runnableing count:" + i);
}
}
}.run();
System.out.println("结束测试多线程");

开始测试多线程
===Runnableing count:0
===Runnableing count:1
===Runnableing count:2
===Runnableing count:3
===Runnableing count:4
结束测试多线程

posted @ 2021-05-11 14:01  syingBlog  阅读(492)  评论(0编辑  收藏  举报