摘要
选用类继承Thread类的方式来创建线程,必须要重写Thread类的run()方法——run()方法是所创建的新线程的执行体,想要新线程执行的操作就写在run()方法中。
当然Java程序开始运行的时候,程序都会有一个主线程(默认名字为main),主线程的执行体就是main()函数——就是所谓的public static void main(){}。
代码块:
接下来是《疯狂Java讲义》给的示例代码(我做了修改,但是不影响功能演示):
package threads;
public class CreateThreads extends Thread{
private int i;
@Override
public void run() {
for(; i<100; i++) {
// 当线程类继承Thread类时,直接使用this即可获取当前线程
// Thread对象的getName()返回调用该方法的线程的名字
// 因此可以直接调用getName()方法返回当前线程的名字,使用getName或者this.getName()都行
/**当然,程序员也可以通过Thread.setName()方法来为线程起名字;默认获取到的线程名字是Thread0、Thread1……等等。*/
//this.setName("TheThreads");
System.out.println(this.getName()+" "+i);
}
}
public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
// 调用Thread的currentThread()方法获取到当前线程
// 在新线程被创建之前,这里打印出的都是主线程——main,main方法就是主线程的线程执行体
System.out.println(Thread.currentThread().getName()
+ " "+i);
if(i == 20) {
CreateThreads t1 = new CreateThreads();
CreateThreads t2 = new CreateThreads();
t1.setName("Thread1");
t2.setName("Thread2");
t1.start();
t2.start();
}
}
}
}
执行结果(包含三次结果,有点长,不想看的小伙伴可以直接跳过):
结果一:
main 0
main 1
main 2
main 3
main 4
main 5
main 6
main 7
main 8
main 9
main 10
main 11
main 12
main 13
main 14
main 15
main 16
main 17
main 18
main 19
main 20
main 21
main 22
main 23
main 24
main 25
main 26
main 27
main 28
main 29
main 30
main 31
Thread1 0
Thread2 0
Thread2 1
Thread2 2
Thread2 3
main 32
Thread2 4
Thread2 5
Thread1 1
Thread2 6
main 33
Thread2 7
Thread1 2
Thread2 8
Thread2 9
Thread2 10
Thread2 11
main 34
Thread2 12
Thread1 3
Thread2 13
main 35
Thread2 14
Thread1 4
Thread2 15
main 36
Thread2 16
Thread1 5
Thread2 17
main 37
Thread2 18
Thread1 6
Thread2 19
main 38
Thread2 20
Thread1 7
Thread2 21
main 39
main 40
Thread2 22
Thread1 8
Thread2 23
main 41
Thread2 24
Thread1 9
Thread2 25
main 42
Thread2 26
Thread2 27
Thread1 10
Thread2 28
Thread2 29
Thread2 30
main 43
main 44
main 45
main 46
main 47
main 48
main 49
main 50
main 51
main 52
main 53
main 54
main 55
main 56
Thread2 31
Thread2 32
Thread1 11
Thread1 12
Thread2 33
main 57
Thread2 34
Thread1 13
Thread2 35
main 58
Thread2 36
Thread2 37
Thread2 38
Thread2 39
Thread2 40
Thread2 41
Thread2 42
Thread2 43
Thread2 44
Thread2 45
Thread2 46
Thread2 47
Thread2 48
Thread2 49
Thread2 50
Thread2 51
Thread2 52
Thread2 53
Thread2 54
Thread2 55
Thread1 14
Thread2 56
main 59
Thread2 57
Thread1 15
Thread2 58
main 60
Thread2 59
Thread1 16
Thread2 60
main 61
Thread2 61
Thread1 17
Thread2 62
main 62
Thread2 63
Thread1 18
Thread2 64
Thread2 65
Thread2 66
Thread2 67
Thread2 68
Thread2 69
Thread2 70
Thread2 71
Thread2 72
main 63
Thread2 73
Thread2 74
Thread1 19
Thread2 75
main 64
main 65
Thread2 76
Thread1 20
Thread2 77
main 66
Thread2 78
Thread1 21
Thread2 79
main 67
Thread2 80
Thread1 22
Thread2 81
main 68
Thread2 82
Thread1 23
Thread2 83
main 69
Thread2 84
Thread1 24
Thread1 25
Thread2 85
main 70
Thread2 86
Thread1 26
Thread2 87
main 71
main 72
main 73
Thread2 88
Thread1 27
Thread2 89
main 74
Thread2 90
Thread1 28
Thread2 91
main 75
Thread2 92
Thread1 29
Thread1 30
Thread1 31
Thread2 93
main 76
Thread2 94
Thread1 32
Thread2 95
main 77
Thread2 96
Thread1 33
Thread2 97
main 78
Thread2 98
Thread1 34
Thread2 99
main 79
Thread1 35
main 80
Thread1 36
main 81
Thread1 37
main 82
Thread1 38
main 83
Thread1 39
main 84
Thread1 40
main 85
Thread1 41
main 86
Thread1 42
main 87
Thread1 43
main 88
Thread1 44
main 89
Thread1 45
main 90
Thread1 46
main 91
Thread1 47
main 92
Thread1 48
main 93
Thread1 49
main 94
Thread1 50
main 95
Thread1 51
main 96
Thread1 52
main 97
Thread1 53
main 98
Thread1 54
main 99
Thread1 55
Thread1 56
Thread1 57
Thread1 58
Thread1 59
Thread1 60
Thread1 61
Thread1 62
Thread1 63
Thread1 64
Thread1 65
Thread1 66
Thread1 67
Thread1 68
Thread1 69
Thread1 70
Thread1 71
Thread1 72
Thread1 73
Thread1 74
Thread1 75
Thread1 76
Thread1 77
Thread1 78
Thread1 79
Thread1 80
Thread1 81
Thread1 82
Thread1 83
Thread1 84
Thread1 85
Thread1 86
Thread1 87
Thread1 88
Thread1 89
Thread1 90
Thread1 91
Thread1 92
Thread1 93
Thread1 94
Thread1 95
Thread1 96
Thread1 97
Thread1 98
Thread1 99
结果二
main 0
main 1
main 2
main 3
main 4
main 5
main 6
main 7
main 8
main 9
main 10
main 11
main 12
main 13
main 14
main 15
main 16
main 17
main 18
main 19
main 20
main 21
main 22
main 23
Thread2 0
Thread2 1
Thread2 2
Thread1 0
Thread2 3
main 24
main 25
main 26
main 27
main 28
main 29
Thread2 4
Thread1 1
Thread2 5
main 30
Thread2 6
Thread1 2
Thread2 7
Thread2 8
Thread2 9
main 31
Thread2 10
Thread2 11
Thread1 3
Thread1 4
Thread2 12
main 32
Thread2 13
Thread2 14
Thread2 15
Thread2 16
Thread2 17
Thread2 18
Thread1 5
Thread1 6
Thread1 7
Thread1 8
Thread1 9
Thread2 19
main 33
Thread2 20
Thread1 10
Thread2 21
main 34
Thread2 22
Thread1 11
Thread2 23
main 35
main 36
main 37
Thread2 24
Thread2 25
Thread2 26
Thread2 27
Thread2 28
Thread2 29
Thread2 30
Thread2 31
Thread1 12
Thread2 32
main 38
Thread2 33
Thread1 13
Thread1 14
Thread1 15
Thread1 16
Thread1 17
Thread1 18
Thread1 19
Thread1 20
Thread1 21
Thread2 34
main 39
Thread2 35
Thread2 36
Thread1 22
Thread2 37
Thread2 38
Thread2 39
Thread2 40
Thread2 41
main 40
Thread2 42
Thread1 23
Thread2 43
main 41
Thread2 44
Thread2 45
Thread1 24
Thread2 46
main 42
Thread2 47
Thread1 25
Thread2 48
main 43
Thread2 49
Thread1 26
Thread2 50
main 44
Thread2 51
Thread1 27
Thread2 52
main 45
Thread2 53
Thread1 28
Thread2 54
main 46
Thread2 55
Thread1 29
Thread2 56
main 47
Thread2 57
Thread1 30
Thread2 58
main 48
Thread2 59
Thread1 31
Thread2 60
main 49
Thread2 61
Thread1 32
Thread1 33
Thread1 34
Thread1 35
Thread2 62
main 50
main 51
main 52
main 53
main 54
main 55
main 56
main 57
main 58
main 59
main 60
main 61
main 62
Thread2 63
Thread1 36
Thread2 64
main 63
Thread2 65
Thread1 37
Thread2 66
main 64
Thread2 67
Thread1 38
Thread2 68
main 65
Thread2 69
Thread1 39
Thread2 70
main 66
Thread2 71
Thread1 40
Thread2 72
main 67
Thread2 73
Thread1 41
Thread2 74
main 68
Thread2 75
Thread1 42
Thread1 43
Thread1 44
Thread1 45
Thread1 46
Thread1 47
Thread1 48
Thread1 49
Thread1 50
Thread1 51
Thread1 52
Thread1 53
Thread1 54
Thread1 55
Thread1 56
Thread1 57
Thread1 58
Thread1 59
Thread1 60
Thread1 61
Thread1 62
Thread1 63
Thread1 64
Thread1 65
Thread1 66
Thread1 67
Thread1 68
Thread2 76
main 69
main 70
main 71
main 72
main 73
main 74
main 75
main 76
Thread2 77
Thread1 69
Thread2 78
main 77
Thread2 79
Thread1 70
Thread2 80
main 78
Thread2 81
Thread1 71
Thread2 82
main 79
Thread2 83
Thread1 72
Thread2 84
main 80
Thread2 85
Thread1 73
Thread2 86
main 81
Thread2 87
Thread1 74
Thread2 88
main 82
Thread2 89
Thread1 75
Thread1 76
Thread2 90
main 83
Thread2 91
Thread1 77
Thread2 92
main 84
Thread2 93
Thread1 78
Thread2 94
main 85
Thread2 95
Thread1 79
Thread2 96
main 86
Thread2 97
Thread1 80
Thread2 98
main 87
Thread2 99
Thread1 81
Thread1 82
main 88
Thread1 83
Thread1 84
main 89
Thread1 85
main 90
Thread1 86
main 91
Thread1 87
Thread1 88
main 92
Thread1 89
main 93
Thread1 90
main 94
main 95
main 96
Thread1 91
main 97
Thread1 92
Thread1 93
main 98
Thread1 94
main 99
Thread1 95
Thread1 96
Thread1 97
Thread1 98
Thread1 99
结果三
main 0
main 1
main 2
main 3
main 4
main 5
main 6
main 7
main 8
main 9
main 10
main 11
main 12
main 13
main 14
main 15
main 16
main 17
main 18
main 19
main 20
main 21
main 22
main 23
main 24
main 25
Thread1 0
main 26
main 27
main 28
main 29
main 30
main 31
main 32
Thread1 1
main 33
Thread1 2
main 34
Thread1 3
main 35
Thread1 4
main 36
Thread1 5
main 37
Thread1 6
main 38
Thread1 7
main 39
Thread1 8
main 40
Thread1 9
main 41
Thread1 10
main 42
Thread1 11
main 43
Thread1 12
Thread2 0
main 44
Thread2 1
Thread1 13
Thread2 2
main 45
Thread2 3
Thread1 14
Thread2 4
main 46
Thread2 5
Thread1 15
Thread2 6
main 47
Thread2 7
Thread1 16
Thread2 8
main 48
Thread2 9
Thread1 17
Thread2 10
main 49
Thread2 11
Thread1 18
Thread2 12
main 50
Thread2 13
Thread1 19
Thread2 14
Thread2 15
Thread2 16
main 51
Thread2 17
Thread1 20
Thread2 18
main 52
Thread2 19
Thread1 21
Thread2 20
main 53
Thread2 21
Thread1 22
Thread2 22
main 54
Thread2 23
Thread2 24
Thread2 25
Thread2 26
Thread2 27
Thread2 28
Thread1 23
Thread2 29
main 55
Thread2 30
Thread2 31
Thread2 32
Thread2 33
Thread2 34
Thread2 35
Thread2 36
Thread2 37
Thread2 38
Thread2 39
Thread2 40
Thread2 41
Thread2 42
Thread2 43
Thread2 44
Thread2 45
Thread2 46
Thread2 47
Thread2 48
Thread2 49
Thread2 50
Thread2 51
Thread2 52
Thread2 53
Thread2 54
Thread2 55
Thread2 56
Thread2 57
Thread2 58
Thread2 59
Thread2 60
Thread2 61
Thread2 62
Thread2 63
Thread2 64
Thread2 65
Thread2 66
Thread2 67
Thread2 68
Thread2 69
Thread2 70
Thread2 71
Thread2 72
Thread2 73
Thread2 74
Thread2 75
Thread2 76
Thread2 77
Thread2 78
Thread2 79
Thread2 80
Thread2 81
Thread2 82
Thread2 83
Thread2 84
Thread2 85
Thread2 86
Thread2 87
Thread2 88
Thread2 89
Thread2 90
Thread2 91
Thread2 92
Thread2 93
Thread2 94
Thread2 95
Thread2 96
Thread2 97
Thread2 98
Thread2 99
Thread1 24
Thread1 25
Thread1 26
Thread1 27
Thread1 28
Thread1 29
Thread1 30
Thread1 31
Thread1 32
Thread1 33
Thread1 34
Thread1 35
Thread1 36
Thread1 37
Thread1 38
Thread1 39
Thread1 40
main 56
Thread1 41
main 57
Thread1 42
main 58
Thread1 43
main 59
Thread1 44
main 60
Thread1 45
main 61
main 62
main 63
Thread1 46
main 64
Thread1 47
main 65
Thread1 48
main 66
Thread1 49
main 67
Thread1 50
main 68
main 69
main 70
main 71
main 72
main 73
main 74
main 75
main 76
main 77
main 78
main 79
Thread1 51
main 80
Thread1 52
main 81
Thread1 53
main 82
Thread1 54
main 83
Thread1 55
main 84
Thread1 56
main 85
Thread1 57
main 86
Thread1 58
Thread1 59
Thread1 60
Thread1 61
Thread1 62
Thread1 63
Thread1 64
Thread1 65
Thread1 66
Thread1 67
Thread1 68
main 87
Thread1 69
main 88
Thread1 70
main 89
main 90
main 91
Thread1 71
Thread1 72
Thread1 73
Thread1 74
Thread1 75
Thread1 76
Thread1 77
main 92
Thread1 78
Thread1 79
Thread1 80
Thread1 81
Thread1 82
main 93
Thread1 83
Thread1 84
Thread1 85
Thread1 86
Thread1 87
Thread1 88
Thread1 89
Thread1 90
Thread1 91
Thread1 92
Thread1 93
Thread1 94
Thread1 95
Thread1 96
Thread1 97
Thread1 98
Thread1 99
main 94
main 95
main 96
main 97
main 98
main 99
总结
这里逻辑作者写的并不难(虽然我也是对着Console输出读了好一会儿才明白,真的好菜~)。
首先,在main函数中有个循环,循环中执行的操作是不断输出当前的线程名字(这里使用了Thread类的getName()方法获取当前线程名,默认是主线程,名字默认是main)。循环到第二十次的时候,开始创建并启动两个线程。
接着在新线程执行体run()中,也是一个100次循环,执行的操作也是输出当前的线程名字。当然,根据主线程中的逻辑——只有主线程的循环(从0开始)执行到第20次的时候,run()才会执行。
也就是说,这个程序中有了三个线程(一个主线程,两个新建的线程),而且干的事情都一样(输出各自的线程名),并且主线程还要领先了20(0~19)次,但是结果是三个线程都从0到100执行完了(我点了好几次执行,**运行结果都是这样,输出的main、Thread1和Thread2后的数字都达到了99)。而且这仨还不一定是谁先干完的。**或许这就是多线程的魅力所在——一倍的时间干三倍的事。
而且,这里大家仔细观察结果也会发现,虽然代码中是从第二十次就开始执行线程,但是输出结果中,Thread1或者Thread2的名字都是从main输出20多次之后才出现的,而不是从第20次就出现的。这让我想起了之前学C#的时候问过老师的一个问题:线程被创建后,调用start()方法是不是就得到执行了呢?答案应该是否。
线程的启动和执行不是一个概念,调用start()方法线程是启动了,但是线程是否得到执行,还要看本机的调度,特别是线程任务很多的时候。也就是说,创建线程并启动之后,线程并不一定就立即得到了执行。就像开车,转动车钥匙,车是启动了,但是车会不会跑起来,还看你这个时候到底想不想开。
最后,在下小白,水平很有限,漏错难免,欢迎本站各位大佬们批评指正;
如果能够帮到一些朋友,不胜荣幸。