Java start和run启动线程的区别
我们知道,我们通过调用线程的start方法启动一个线程,那么,我们可以直接调用run方法来启动一个线程吗?
先看下面一段代码:
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TestThread tt = new TestThread();
- tt.run();
- }
- }
- class TestThread extends Thread {
- static int i = 0;
- final static int MAX_I = 10;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while (i < MAX_I) {
- System.out.println(i++);
- }
- }
- }
运行结果如下:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
或许有人会得出结论,这样启动一个线程是可以的,我们再对程式稍做修改,大家就会发现一个问题:
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TestThread tt = new TestThread();
- tt.run();
- System.out.println("Printed by main thread");
- }
- }
- class TestThread extends Thread {
- static int i = 0;
- final static int MAX_I = 10;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while (i < MAX_I) {
- System.out.println(i++);
- }
- }
- }
这里只在主线程中加入了一行代码,打印一行"Printed by main thread",运行代码,结果如下:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- Printed by main thread
熟练多线程开发的要发现问题了,为什么"Printed by main thread"会打印在最后一行呢?TestThread类中一直持有时间段吗?
我们对上面的代码进行分析,其实非常简单,这只是一个普通的类中方法的调用,其实是一个单线程的执行,我们来修改代码进一步验证这一点:
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TestThread tt = new TestThread();
- tt.run();
- System.out.println(Thread.currentThread().getName());
- System.out.println("Printed by main thread");
- }
- }
- class TestThread extends Thread {
- static int i = 0;
- final static int MAX_I = 10;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println(Thread.currentThread().getName());
- while (i < MAX_I) {
- System.out.println(i++);
- }
- }
- }
这段代码分别在主线程和我们的TestThread的方法中打印当前线程名字,运行结果如下:
- main
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- main
- Printed by main thread
在TestThread类和主线程中运行的是同一个线程,说明在直接调用run时是不能使用多线程的,那么把上面的run方法调用改为start方法的调动再看一下:
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TestThread tt = new TestThread();
- tt.start();
- System.out.println(Thread.currentThread().getName());
- System.out.println("Printed by main thread");
- }
- }
- class TestThread extends Thread {
- static int i = 0;
- final static int MAX_I = 10;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println(Thread.currentThread().getName());
- while (i < MAX_I) {
- System.out.println(i++);
- }
- }
- }
运行结果如下:
- main
- Thread-0
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- Printed by main thread
- 9
很明显,这才是我们想看到的结果,所以结论是只有调用Thread的start方法,将线程交由JVM控制,才能产生多线程,而直接调用run方法只是一个普通的单线程程式。
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了