run() 和 start() 的区别
1) start:
用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面(指主线程下面)的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。
1 package com.mianshi.easy; 2 public class StartRunTest { 3 /** 4 * 直接调用run()和用start()启动一个线程的差别 5 */ 6 7 public static void main(String[] args){ 8 Thread thread=new ThreadDemo(); 9 //第一种 10 //表明: run()和其他方法的调用没任何不同,main方法按顺序执行了它,并打印出最后一句 11 /*thread.run(); 12 for(int i=0;i<100;i++){ 13 System.out.println(i); 14 }*/ 15 16 //第二种 17 //表明: start()方法重新创建了一个线程,在main方法执行结束后,由于start()方法创建的线程没有运行结束, 18 //因此主线程未能退出,直到线程thread也执行完毕.这里要注意,默认创建的线程是用户线程(非守护线程) 19 thread.start(); 20 for(int i=0;i<100;i++){ 21 System.out.println(i); 22 } 23 24 //第三种 25 //1、为什么没有打印出100句呢?因为我们将thread线程设置为了daemon(守护)线程,程序中只有守护线程存在的时候,是可以退出的,所以只打印了七句便退出了 26 //2、当java虚拟机中有守护线程在运行的时候,java虚拟机会关闭。当所有常规线程运行完毕以后, 27 //守护线程不管运行到哪里,虚拟机都会退出运行。所以你的守护线程最好不要写一些会影响程序的业务逻辑。否则无法预料程序到底会出现什么问题 28 /*thread.setDaemon(true); 29 thread.start();*/ 30 31 //第四种 32 //用户线程可以被System.exit(0)强制kill掉,所以也只打印出七句 33 /* thread.start(); 34 System.out.println("main thread is over"); 35 System.exit(1);*/ 36 } 37 38 public static class ThreadDemo extends Thread{ 39 @Override 40 public void run() { 41 for (int i = 0; i < 100; i++) { 42 System.out.println("This is a Thread test"+i); 43 } 44 } 45 } 46 }
结果:
0 1 2 3 4 5 6 7 This is a Thread test0 This is a Thread test1 This is a Thread test2 This is a Thread test3 This is a Thread test4 8 This is a Thread test5 This is a Thread test6 This is a Thread test7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 This is a Thread test8 This is a Thread test9 This is a Thread test10 This is a Thread test11 This is a Thread test12 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 This is a Thread test13 78 This is a Thread test14 This is a Thread test15 This is a Thread test16 This is a Thread test17 This is a Thread test18 This is a Thread test19 This is a Thread test20 This is a Thread test21 This is a Thread test22 This is a Thread test23 This is a Thread test24 This is a Thread test25 This is a Thread test26 This is a Thread test27 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 This is a Thread test28 This is a Thread test29 This is a Thread test30 This is a Thread test31 This is a Thread test32 This is a Thread test33 This is a Thread test34 This is a Thread test35 This is a Thread test36 This is a Thread test37 This is a Thread test38 This is a Thread test39 This is a Thread test40 This is a Thread test41 This is a Thread test42 This is a Thread test43 This is a Thread test44 This is a Thread test45 This is a Thread test46 This is a Thread test47 This is a Thread test48 This is a Thread test49 This is a Thread test50 This is a Thread test51 This is a Thread test52 This is a Thread test53 This is a Thread test54 This is a Thread test55 This is a Thread test56 This is a Thread test57 This is a Thread test58 This is a Thread test59 This is a Thread test60 This is a Thread test61 This is a Thread test62 This is a Thread test63 This is a Thread test64 This is a Thread test65 This is a Thread test66 This is a Thread test67 This is a Thread test68 This is a Thread test69 This is a Thread test70 This is a Thread test71 This is a Thread test72 This is a Thread test73 This is a Thread test74 This is a Thread test75 This is a Thread test76 This is a Thread test77 This is a Thread test78 This is a Thread test79 This is a Thread test80 This is a Thread test81 This is a Thread test82 This is a Thread test83 This is a Thread test84 This is a Thread test85 This is a Thread test86 This is a Thread test87 This is a Thread test88 This is a Thread test89 This is a Thread test90 This is a Thread test91 This is a Thread test92 This is a Thread test93 This is a Thread test94 This is a Thread test95 This is a Thread test96 This is a Thread test97 This is a Thread test98 This is a Thread test99
子线程成功创建,主线程的代码也在继续运行,而且证明了子线程短暂等待CPU时间片的过程,此过程中主线程没有停止。
2) run:
run()方法只是类的一个普通方法而已,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到写线程的目的。
1 package com.mianshi.easy; 2 public class StartRunTest { 3 /** 4 * 直接调用run()和用start()启动一个线程的差别 5 */ 6 7 public static void main(String[] args){ 8 Thread thread=new ThreadDemo(); 9 //第一种 10 //表明: run()和其他方法的调用没任何不同,main方法按顺序执行了它,并打印出最后一句 11 thread.run(); 12 for(int i=0;i<100;i++){ 13 System.out.println(i); 14 } 15 16 //第二种 17 //表明: start()方法重新创建了一个线程,在main方法执行结束后,由于start()方法创建的线程没有运行结束, 18 //因此主线程未能退出,直到线程thread也执行完毕.这里要注意,默认创建的线程是用户线程(非守护线程) 19 /*thread.start(); 20 for(int i=0;i<100;i++){ 21 System.out.println(i); 22 }*/ 23 24 //第三种 25 //1、为什么没有打印出100句呢?因为我们将thread线程设置为了daemon(守护)线程,程序中只有守护线程存在的时候,是可以退出的,所以只打印了七句便退出了 26 //2、当java虚拟机中有守护线程在运行的时候,java虚拟机会关闭。当所有常规线程运行完毕以后, 27 //守护线程不管运行到哪里,虚拟机都会退出运行。所以你的守护线程最好不要写一些会影响程序的业务逻辑。否则无法预料程序到底会出现什么问题 28 /*thread.setDaemon(true); 29 thread.start();*/ 30 31 //第四种 32 //用户线程可以被System.exit(0)强制kill掉,所以也只打印出七句 33 /* thread.start(); 34 System.out.println("main thread is over"); 35 System.exit(1);*/ 36 } 37 38 public static class ThreadDemo extends Thread{ 39 @Override 40 public void run() { 41 for (int i = 0; i < 100; i++) { 42 System.out.println("This is a Thread test"+i); 43 } 44 } 45 } 46 }
结果:
This is a Thread test0
This is a Thread test1
This is a Thread test2
This is a Thread test3
This is a Thread test4
This is a Thread test5
This is a Thread test6
This is a Thread test7
This is a Thread test8
This is a Thread test9
This is a Thread test10
This is a Thread test11
This is a Thread test12
This is a Thread test13
This is a Thread test14
This is a Thread test15
This is a Thread test16
This is a Thread test17
This is a Thread test18
This is a Thread test19
This is a Thread test20
This is a Thread test21
This is a Thread test22
This is a Thread test23
This is a Thread test24
This is a Thread test25
This is a Thread test26
This is a Thread test27
This is a Thread test28
This is a Thread test29
This is a Thread test30
This is a Thread test31
This is a Thread test32
This is a Thread test33
This is a Thread test34
This is a Thread test35
This is a Thread test36
This is a Thread test37
This is a Thread test38
This is a Thread test39
This is a Thread test40
This is a Thread test41
This is a Thread test42
This is a Thread test43
This is a Thread test44
This is a Thread test45
This is a Thread test46
This is a Thread test47
This is a Thread test48
This is a Thread test49
This is a Thread test50
This is a Thread test51
This is a Thread test52
This is a Thread test53
This is a Thread test54
This is a Thread test55
This is a Thread test56
This is a Thread test57
This is a Thread test58
This is a Thread test59
This is a Thread test60
This is a Thread test61
This is a Thread test62
This is a Thread test63
This is a Thread test64
This is a Thread test65
This is a Thread test66
This is a Thread test67
This is a Thread test68
This is a Thread test69
This is a Thread test70
This is a Thread test71
This is a Thread test72
This is a Thread test73
This is a Thread test74
This is a Thread test75
This is a Thread test76
This is a Thread test77
This is a Thread test78
This is a Thread test79
This is a Thread test80
This is a Thread test81
This is a Thread test82
This is a Thread test83
This is a Thread test84
This is a Thread test85
This is a Thread test86
This is a Thread test87
This is a Thread test88
This is a Thread test89
This is a Thread test90
This is a Thread test91
This is a Thread test92
This is a Thread test93
This is a Thread test94
This is a Thread test95
This is a Thread test96
This is a Thread test97
This is a Thread test98
This is a Thread test99
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
并没有产生子线程,而是普通的方法调用,只有等调用方法执行完后,主线程才能继续执行处后面的代码。
总结:调用start方法方可启动线程,而run方法只是thread的一个普通方法调用,还是在主线程里执行。