java线程基础巩固---线程ID,优先级

这里学习Thread的两个比较简单的API,直接上代码:

线程ID:

那它的生成规则是?直接看源码:

那为什么目前打印是9呢?然后在jvm启动的时候就已经创建了8个线程?继续用jconsole来验证一下:

启动jconsole:

优先级:

我们在学习java线程中都知道这个优先级在实际中是不受控的,也就是说要控制线程的优先级不能通过它的API来,而应该是从咱们的逻辑上去实现,但是这里还是做一个了解,试一下它的效果:

编译运行:

Thread-0-index0
Thread-0-index1
Thread-0-index2
Thread-0-index3
Thread-0-index4
Thread-0-index5
Thread-0-index6
Thread-0-index7
Thread-0-index8
Thread-0-index9
Thread-1-index0
Thread-1-index1
Thread-1-index2
Thread-0-index10
Thread-1-index3
Thread-1-index4
Thread-1-index5
Thread-1-index6
Thread-0-index11
Thread-2-index0
Thread-2-index1
Thread-2-index2
Thread-2-index3
Thread-1-index7
Thread-1-index8
Thread-1-index9
Thread-2-index4
Thread-2-index5
Thread-2-index6
Thread-0-index12
Thread-0-index13
Thread-0-index14
Thread-2-index7
Thread-1-index10
Thread-1-index11
Thread-1-index12
Thread-1-index13
Thread-1-index14
Thread-1-index15
Thread-1-index16
Thread-2-index8
Thread-2-index9
Thread-0-index15
Thread-2-index10
Thread-2-index11
Thread-2-index12
Thread-2-index13
Thread-2-index14
Thread-2-index15
Thread-2-index16
Thread-2-index17
Thread-2-index18
Thread-2-index19
Thread-2-index20
Thread-2-index21
Thread-2-index22
Thread-2-index23
Thread-2-index24
Thread-1-index17
Thread-2-index25
Thread-0-index16
Thread-2-index26
Thread-1-index18
Thread-2-index27
Thread-0-index17
Thread-2-index28
Thread-2-index29
Thread-2-index30
Thread-2-index31
Thread-2-index32
Thread-2-index33
Thread-2-index34
Thread-2-index35
Thread-2-index36
Thread-2-index37
Thread-1-index19
Thread-2-index38
Thread-2-index39
Thread-2-index40
Thread-2-index41
Thread-2-index42
Thread-2-index43
Thread-2-index44
Thread-0-index18
Thread-2-index45
Thread-2-index46
Thread-1-index20
Thread-1-index21
Thread-2-index47
Thread-0-index19
Thread-2-index48
Thread-1-index22
Thread-2-index49
Thread-2-index50
Thread-2-index51
Thread-2-index52
Thread-0-index20
Thread-2-index53
Thread-1-index23
Thread-1-index24
Thread-2-index54
Thread-0-index21
Thread-2-index55
Thread-2-index56
Thread-1-index25
Thread-2-index57
Thread-2-index58
Thread-2-index59
Thread-2-index60
Thread-2-index61
Thread-2-index62
Thread-2-index63
Thread-2-index64
Thread-2-index65
Thread-2-index66
Thread-2-index67
Thread-2-index68
Thread-0-index22
Thread-0-index23
Thread-0-index24
Thread-0-index25
Thread-2-index69
Thread-1-index26
Thread-1-index27
Thread-1-index28
Thread-2-index70
Thread-2-index71
Thread-2-index72
Thread-0-index26
Thread-2-index73
Thread-1-index29
Thread-1-index30
Thread-1-index31
Thread-1-index32
Thread-1-index33
Thread-1-index34
Thread-2-index74
Thread-0-index27
Thread-0-index28
Thread-0-index29
Thread-2-index75
Thread-1-index35
Thread-2-index76
Thread-0-index30
Thread-0-index31
Thread-0-index32
Thread-2-index77
Thread-2-index78
Thread-2-index79
Thread-2-index80
Thread-2-index81
Thread-2-index82
Thread-2-index83
Thread-2-index84
Thread-2-index85
Thread-1-index36
Thread-2-index86
Thread-0-index33
Thread-0-index34
Thread-0-index35
Thread-0-index36
Thread-0-index37
Thread-0-index38
Thread-0-index39
Thread-0-index40
Thread-0-index41
Thread-0-index42
Thread-0-index43
Thread-0-index44
Thread-0-index45
Thread-2-index87
Thread-1-index37
Thread-1-index38
Thread-2-index88
Thread-0-index46
Thread-0-index47
Thread-0-index48
Thread-0-index49
Thread-0-index50
Thread-0-index51
Thread-0-index52
Thread-0-index53
Thread-0-index54
Thread-2-index89
Thread-1-index39
Thread-1-index40
Thread-1-index41
Thread-1-index42
Thread-1-index43
Thread-2-index90
Thread-2-index91
Thread-2-index92
Thread-0-index55
Thread-2-index93
Thread-2-index94
Thread-1-index44
Thread-2-index95
Thread-2-index96
Thread-2-index97
Thread-2-index98
Thread-2-index99
Thread-0-index56
Thread-1-index45
Thread-1-index46
Thread-1-index47
Thread-1-index48
Thread-1-index49
Thread-1-index50
Thread-1-index51
Thread-1-index52
Thread-1-index53
Thread-1-index54
Thread-1-index55
Thread-1-index56
Thread-1-index57
Thread-1-index58
Thread-1-index59
Thread-0-index57
Thread-1-index60
Thread-1-index61
Thread-1-index62
Thread-1-index63
Thread-1-index64
Thread-1-index65
Thread-1-index66
Thread-1-index67
Thread-1-index68
Thread-1-index69
Thread-1-index70
Thread-1-index71
Thread-1-index72
Thread-1-index73
Thread-1-index74
Thread-1-index75
Thread-1-index76
Thread-1-index77
Thread-1-index78
Thread-1-index79
Thread-1-index80
Thread-1-index81
Thread-1-index82
Thread-1-index83
Thread-1-index84
Thread-1-index85
Thread-1-index86
Thread-1-index87
Thread-1-index88
Thread-1-index89
Thread-1-index90
Thread-0-index58
Thread-1-index91
Thread-0-index59
Thread-0-index60
Thread-1-index92
Thread-0-index61
Thread-0-index62
Thread-0-index63
Thread-1-index93
Thread-0-index64
Thread-1-index94
Thread-0-index65
Thread-0-index66
Thread-0-index67
Thread-0-index68
Thread-0-index69
Thread-1-index95
Thread-0-index70
Thread-0-index71
Thread-0-index72
Thread-0-index73
Thread-0-index74
Thread-0-index75
Thread-1-index96
Thread-0-index76
Thread-0-index77
Thread-1-index97
Thread-0-index78
Thread-1-index98
Thread-1-index99
Thread-0-index79
Thread-0-index80
Thread-0-index81
Thread-0-index82
Thread-0-index83
Thread-0-index84
Thread-0-index85
Thread-0-index86
Thread-0-index87
Thread-0-index88
Thread-0-index89
Thread-0-index90
Thread-0-index91
Thread-0-index92
Thread-0-index93
Thread-0-index94
Thread-0-index95
Thread-0-index96
Thread-0-index97
Thread-0-index98
Thread-0-index99

当然每次执行可能不一样,这时设置一下各线程中的优先级:

编译运行:

Thread-0-index0
Thread-0-index1
Thread-0-index2
Thread-0-index3
Thread-0-index4
Thread-0-index5
Thread-0-index6
Thread-0-index7
Thread-0-index8
Thread-0-index9
Thread-0-index10
Thread-0-index11
Thread-0-index12
Thread-0-index13
Thread-0-index14
Thread-0-index15
Thread-0-index16
Thread-0-index17
Thread-0-index18
Thread-0-index19
Thread-0-index20
Thread-0-index21
Thread-0-index22
Thread-0-index23
Thread-0-index24
Thread-0-index25
Thread-0-index26
Thread-0-index27
Thread-0-index28
Thread-0-index29
Thread-0-index30
Thread-0-index31
Thread-1-index0
Thread-1-index1
Thread-0-index32
Thread-1-index2
Thread-1-index3
Thread-1-index4
Thread-0-index33
Thread-1-index5
Thread-0-index34
Thread-1-index6
Thread-1-index7
Thread-1-index8
Thread-1-index9
Thread-0-index35
Thread-1-index10
Thread-2-index0
Thread-2-index1
Thread-1-index11
Thread-0-index36
Thread-0-index37
Thread-0-index38
Thread-0-index39
Thread-1-index12
Thread-1-index13
Thread-1-index14
Thread-2-index2
Thread-1-index15
Thread-1-index16
Thread-1-index17
Thread-1-index18
Thread-1-index19
Thread-1-index20
Thread-0-index40
Thread-1-index21
Thread-2-index3
Thread-1-index22
Thread-0-index41
Thread-1-index23
Thread-2-index4
Thread-1-index24
Thread-1-index25
Thread-1-index26
Thread-1-index27
Thread-0-index42
Thread-1-index28
Thread-2-index5
Thread-2-index6
Thread-1-index29
Thread-1-index30
Thread-0-index43
Thread-0-index44
Thread-0-index45
Thread-0-index46
Thread-0-index47
Thread-1-index31
Thread-2-index7
Thread-2-index8
Thread-1-index32
Thread-0-index48
Thread-1-index33
Thread-2-index9
Thread-1-index34
Thread-0-index49
Thread-1-index35
Thread-1-index36
Thread-1-index37
Thread-1-index38
Thread-1-index39
Thread-1-index40
Thread-1-index41
Thread-1-index42
Thread-1-index43
Thread-1-index44
Thread-1-index45
Thread-1-index46
Thread-1-index47
Thread-1-index48
Thread-1-index49
Thread-1-index50
Thread-1-index51
Thread-1-index52
Thread-1-index53
Thread-1-index54
Thread-1-index55
Thread-1-index56
Thread-1-index57
Thread-1-index58
Thread-1-index59
Thread-1-index60
Thread-1-index61
Thread-1-index62
Thread-1-index63
Thread-1-index64
Thread-1-index65
Thread-1-index66
Thread-1-index67
Thread-1-index68
Thread-1-index69
Thread-1-index70
Thread-1-index71
Thread-1-index72
Thread-1-index73
Thread-1-index74
Thread-1-index75
Thread-1-index76
Thread-1-index77
Thread-1-index78
Thread-1-index79
Thread-1-index80
Thread-1-index81
Thread-1-index82
Thread-1-index83
Thread-1-index84
Thread-1-index85
Thread-1-index86
Thread-1-index87
Thread-1-index88
Thread-1-index89
Thread-1-index90
Thread-1-index91
Thread-1-index92
Thread-1-index93
Thread-1-index94
Thread-1-index95
Thread-1-index96
Thread-1-index97
Thread-1-index98
Thread-1-index99
Thread-2-index10
Thread-0-index50
Thread-2-index11
Thread-0-index51
Thread-2-index12
Thread-0-index52
Thread-2-index13
Thread-2-index14
Thread-2-index15
Thread-0-index53
Thread-2-index16
Thread-0-index54
Thread-0-index55
Thread-0-index56
Thread-0-index57
Thread-0-index58
Thread-2-index17
Thread-0-index59
Thread-0-index60
Thread-2-index18
Thread-0-index61
Thread-2-index19
Thread-0-index62
Thread-2-index20
Thread-0-index63
Thread-2-index21
Thread-0-index64
Thread-2-index22
Thread-0-index65
Thread-0-index66
Thread-0-index67
Thread-0-index68
Thread-0-index69
Thread-0-index70
Thread-0-index71
Thread-0-index72
Thread-0-index73
Thread-0-index74
Thread-0-index75
Thread-0-index76
Thread-0-index77
Thread-0-index78
Thread-0-index79
Thread-0-index80
Thread-0-index81
Thread-0-index82
Thread-0-index83
Thread-0-index84
Thread-0-index85
Thread-0-index86
Thread-0-index87
Thread-0-index88
Thread-0-index89
Thread-0-index90
Thread-0-index91
Thread-0-index92
Thread-0-index93
Thread-0-index94
Thread-0-index95
Thread-0-index96
Thread-0-index97
Thread-0-index98
Thread-0-index99
Thread-2-index23
Thread-2-index24
Thread-2-index25
Thread-2-index26
Thread-2-index27
Thread-2-index28
Thread-2-index29
Thread-2-index30
Thread-2-index31
Thread-2-index32
Thread-2-index33
Thread-2-index34
Thread-2-index35
Thread-2-index36
Thread-2-index37
Thread-2-index38
Thread-2-index39
Thread-2-index40
Thread-2-index41
Thread-2-index42
Thread-2-index43
Thread-2-index44
Thread-2-index45
Thread-2-index46
Thread-2-index47
Thread-2-index48
Thread-2-index49
Thread-2-index50
Thread-2-index51
Thread-2-index52
Thread-2-index53
Thread-2-index54
Thread-2-index55
Thread-2-index56
Thread-2-index57
Thread-2-index58
Thread-2-index59
Thread-2-index60
Thread-2-index61
Thread-2-index62
Thread-2-index63
Thread-2-index64
Thread-2-index65
Thread-2-index66
Thread-2-index67
Thread-2-index68
Thread-2-index69
Thread-2-index70
Thread-2-index71
Thread-2-index72
Thread-2-index73
Thread-2-index74
Thread-2-index75
Thread-2-index76
Thread-2-index77
Thread-2-index78
Thread-2-index79
Thread-2-index80
Thread-2-index81
Thread-2-index82
Thread-2-index83
Thread-2-index84
Thread-2-index85
Thread-2-index86
Thread-2-index87
Thread-2-index88
Thread-2-index89
Thread-2-index90
Thread-2-index91
Thread-2-index92
Thread-2-index93
Thread-2-index94
Thread-2-index95
Thread-2-index96
Thread-2-index97
Thread-2-index98
Thread-2-index99

从结果中确实有一些影响,第一个线程先执行了,第二个次之,第三个最后执行,但是基本也是交替进行的,所以这里纯学习试验,实际基本上不会用这个API,了解下。

posted on 2017-11-25 14:58  cexo  阅读(286)  评论(0编辑  收藏  举报

导航