为什么我的java多线程程序是执行完一个再到另外一个的???而不是同时执行的??

2024-12-13 23:41:02
推荐回答(3个)
回答1:

把你的代码复制下来,在我电脑上跑了一下,运行结果如下

Runer1 Thread: 0
Runer1 Thread: 1
Runer1 Thread: 2
Runer1 Thread: 3
Runer1 Thread: 4
Runer1 Thread: 5
Runer1 Thread: 6
Runer1 Thread: 7
Runer1 Thread: 8
Runer1 Thread: 9
main Thread ------0
Runer1 Thread: 10
Runer1 Thread: 11
Runer1 Thread: 12
Runer1 Thread: 13
Runer1 Thread: 14
Runer1 Thread: 15
Runer1 Thread: 16
Runer1 Thread: 17
Runer1 Thread: 18
Runer1 Thread: 19
Runer1 Thread: 20
Runer1 Thread: 21
Runer1 Thread: 22
Runer1 Thread: 23
Runer1 Thread: 24
Runer1 Thread: 25
Runer1 Thread: 26
Runer1 Thread: 27
Runer1 Thread: 28
Runer1 Thread: 29
Runer1 Thread: 30
Runer1 Thread: 31
Runer1 Thread: 32
Runer1 Thread: 33
Runer1 Thread: 34
Runer1 Thread: 35
Runer1 Thread: 36
Runer1 Thread: 37
Runer1 Thread: 38
Runer1 Thread: 39
Runer1 Thread: 40
Runer1 Thread: 41
Runer1 Thread: 42
Runer1 Thread: 43
Runer1 Thread: 44
Runer1 Thread: 45
main Thread ------1
main Thread ------2
main Thread ------3
main Thread ------4
main Thread ------5
main Thread ------6
main Thread ------7
main Thread ------8
main Thread ------9
main Thread ------10
main Thread ------11
main Thread ------12
main Thread ------13
main Thread ------14
main Thread ------15
main Thread ------16
main Thread ------17
main Thread ------18
main Thread ------19
main Thread ------20
main Thread ------21
main Thread ------22
main Thread ------23
main Thread ------24
main Thread ------25
main Thread ------26
main Thread ------27
main Thread ------28
main Thread ------29
main Thread ------30
main Thread ------31
main Thread ------32
main Thread ------33
main Thread ------34
main Thread ------35
main Thread ------36
main Thread ------37
main Thread ------38
main Thread ------39
main Thread ------40
main Thread ------41
main Thread ------42
main Thread ------43
main Thread ------44
main Thread ------45
main Thread ------46
main Thread ------47
main Thread ------48
main Thread ------49
main Thread ------50
main Thread ------51
main Thread ------52
main Thread ------53
main Thread ------54
main Thread ------55
Runer1 Thread: 46
main Thread ------56
Runer1 Thread: 47
main Thread ------57
Runer1 Thread: 48
Runer1 Thread: 49
Runer1 Thread: 50
Runer1 Thread: 51
Runer1 Thread: 52
Runer1 Thread: 53
Runer1 Thread: 54
Runer1 Thread: 55
Runer1 Thread: 56
Runer1 Thread: 57
Runer1 Thread: 58
Runer1 Thread: 59
Runer1 Thread: 60
Runer1 Thread: 61
Runer1 Thread: 62
Runer1 Thread: 63
Runer1 Thread: 64
Runer1 Thread: 65
Runer1 Thread: 66
main Thread ------58
Runer1 Thread: 67
Runer1 Thread: 68
main Thread ------59
Runer1 Thread: 69
main Thread ------60
Runer1 Thread: 70
main Thread ------61
main Thread ------62
main Thread ------63
main Thread ------64
main Thread ------65
main Thread ------66
main Thread ------67
main Thread ------68
main Thread ------69
main Thread ------70
main Thread ------71
main Thread ------72
main Thread ------73
main Thread ------74
main Thread ------75
main Thread ------76
main Thread ------77
Runer1 Thread: 71
Runer1 Thread: 72
Runer1 Thread: 73
Runer1 Thread: 74
Runer1 Thread: 75
Runer1 Thread: 76
Runer1 Thread: 77
Runer1 Thread: 78
Runer1 Thread: 79
Runer1 Thread: 80
Runer1 Thread: 81
Runer1 Thread: 82
Runer1 Thread: 83
Runer1 Thread: 84
Runer1 Thread: 85
Runer1 Thread: 86
Runer1 Thread: 87
Runer1 Thread: 88
main Thread ------78
Runer1 Thread: 89
Runer1 Thread: 90
Runer1 Thread: 91
Runer1 Thread: 92
Runer1 Thread: 93
Runer1 Thread: 94
Runer1 Thread: 95
Runer1 Thread: 96
Runer1 Thread: 97
Runer1 Thread: 98
Runer1 Thread: 99
main Thread ------79
main Thread ------80
main Thread ------81
main Thread ------82
main Thread ------83
main Thread ------84
main Thread ------85
main Thread ------86
main Thread ------87
main Thread ------88
main Thread ------89
main Thread ------90
main Thread ------91
main Thread ------92
main Thread ------93
main Thread ------94
main Thread ------95
main Thread ------96
main Thread ------97
main Thread ------98
main Thread ------99

回答2:

操作太短 在一个时间片里就完成了 ,增加操作次数

回答3:

先告诉你的你的机器是几核的?单核,双核,四核的机器在线程执行上会有很大的区别