我们知道,我们通过调用线程的start方法启动一个线程,那么,我们可以直接调用run方法来启动一个线程吗?
先看下面一段代码:
Java代码
1. public class Test {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. TestThread tt = new TestThread();
5. tt.run();
6. }
7. }
8.
9. class TestThread extends Thread {
10. static int i = 0;
11. final static int MAX_I = 10;
12.
13. @Override
14. public void run() {
15. // TODO Auto-generated method stub
16. while (i < MAX_I) {
17. System.out.println(i++);
18. }
19. }
20. }
运行结果如下:
Java代码
1. 0
2. 1
3. 2
4. 3
5. 4
6. 5
7. 6
8. 7
9. 8
10. 9
或许有人会得出结论,这样启动一个线程是可以的,我们再对程式稍做修改,大家就会发现一个问题:
Java代码
1. public class Test {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. TestThread tt = new TestThread();
5. tt.run();
6. System.out.println("Printed by main thread");
7. }
8. }
9.
10. class TestThread extends Thread {
11. static int i = 0;
12. final static int MAX_I = 10;
13.
14. @Override
15. public void run() {
16. // TODO Auto-generated method stub
17. while (i < MAX_I) {
18. System.out.println(i++);
19. }
20. }
21.
22. }
这里只在主线程中加入了一行代码,打印一行"Printed by main thread",运行代码,结果如下:
Java代码
1. 0
2. 1
3. 2
4. 3
5. 4
6. 5
7. 6
8. 7
9. 8
10. 9
11. Printed by main thread
熟练多线程开发的要发现问题了,为什么"Printed by main thread"会打印在最后一行呢?TestThread类中一直持有时间段吗?
我们对上面的代码进行分析,其实非常简单,这只是一个普通的类中方法的调用,其实是一个单线程的执行,我们来修改代码进一步验证这一点:
Java代码
1. public class Test {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. TestThread tt = new TestThread();
5. tt.run();
6. System.out.println(Thread.currentThread().getName());
7. System.out.println("Printed by main thread");
8. }
9. }
10.
11. class TestThread extends Thread {
12. static int i = 0;
13. final static int MAX_I = 10;
14.
15. @Override
16. public void run() {
17. // TODO Auto-generated method stub
18. System.out.println(Thread.currentThread().getName());
19. while (i < MAX_I) {
20. System.out.println(i++);
21. }
22. }
23. }
这段代码分别在主线程和我们的TestThread的方法中打印当前线程名字,运行结果如下:
Java代码
1. main
2. 0
3. 1
4. 2
5. 3
6. 4
7. 5
8. 6
9. 7
10. 8
11. 9
12. main
13. Printed by main thread
在TestThread类和主线程中运行的是同一个线程,说明在直接调用run时是不能使用多线程的,那么把上面的run方法调用改为start方法的调动再看一下:
Java代码
1. public class Test {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. TestThread tt = new TestThread();
5. tt.start();
6. System.out.println(Thread.currentThread().getName());
7. System.out.println("Printed by main thread");
8. }
9. }
10.
11. class TestThread extends Thread {
12. static int i = 0;
13. final static int MAX_I = 10;
14.
15. @Override
16. public void run() {
17. // TODO Auto-generated method stub
18. System.out.println(Thread.currentThread().getName());
19. while (i < MAX_I) {
20. System.out.println(i++);
21. }
22. }
23. }
运行结果如下:
Java代码
1. main
2. Thread-0
3. 0
4. 1
5. 2
6. 3
7. 4
8. 5
9. 6
10. 7
11. 8
12. Printed by main thread
13. 9
很明显,这才是我们想看到的结果,所以结论是只有调用Thread的start方法,将线程交由JVM控制,才能产生多线程,而直接调用run方法只是一个普通的单线程程式。
Java牛角尖【008】: 可以通过调用一个线程的run方法启动一个线程吗?
最新推荐文章于 2022-07-16 22:42:42 发布