為什么Runnable沒(méi)有運(yùn)行結(jié)果
package?com.imooc.concurrent; public?class?Actor?extends?Thread?{ ????public?void?run(){ ????????System.out.println(getName()+"演出開(kāi)始了!"); ????????int?count?=?0;????????????????//計(jì)數(shù)器 ????????boolean?keepRunning?=?true; ????????while(keepRunning){ ????????????System.out.println(getName()+"登臺(tái)演出:"+(++count)); ????????????if(count?==?100){ ????????????????keepRunning?=?false; ????????????} ????????????if(count%10==0){ ????????????????try?{ ????????????????????Thread.sleep(1000);//設(shè)置停頓時(shí)間 ????????????????}?catch?(InterruptedException?e)?{ ????????????????????e.printStackTrace(); ????????????????} ????????????} ????????} ????????System.out.println(getName()+"演出結(jié)束了!"); ????} ????public?static?void?main(String[]?args)?{ ????????Thread?actor?=?new?Actor(); ????????actor.setName("Mr.Thread"); ????????actor.start(); ????????Thread?actressThread?=?new?Thread(); ????????actressThread.setName("Ms.Runnable"); ????????actressThread.start(); ????} class?Actress?implements?Runnable{ ????????@Override ????????public?void?run()?{ ????????????System.out.println(getName()+"演出開(kāi)始了!"); ????????????int?count?=?0;????????????????//計(jì)數(shù)器 ????????????boolean?keepRunning?=?true; ????????????while(keepRunning){ ????????????????System.out.println(getName()+"登臺(tái)演出:"+(++count)); ????????????????if(count?==?100){ ????????????????????keepRunning?=?false; ????????????????} ????????????????if(count%10==0){ ????????????????????try?{ ????????????????????????Thread.sleep(1000);//設(shè)置停頓時(shí)間 ????????????????????}?catch?(InterruptedException?e)?{ ????????????????????????e.printStackTrace(); ????????????????????} ????????????????} ????????????} ????????????System.out.println(getName()+"演出結(jié)束了!"); ????????} ???????? ????} }
結(jié)果:
Mr.Thread演出開(kāi)始了!
Mr.Thread登臺(tái)演出:1
Mr.Thread登臺(tái)演出:2
Mr.Thread登臺(tái)演出:3
Mr.Thread登臺(tái)演出:4
Mr.Thread登臺(tái)演出:5
Mr.Thread登臺(tái)演出:6
Mr.Thread登臺(tái)演出:7
Mr.Thread登臺(tái)演出:8
Mr.Thread登臺(tái)演出:9
Mr.Thread登臺(tái)演出:10
Mr.Thread登臺(tái)演出:11
Mr.Thread登臺(tái)演出:12
Mr.Thread登臺(tái)演出:13
Mr.Thread登臺(tái)演出:14
Mr.Thread登臺(tái)演出:15
Mr.Thread登臺(tái)演出:16
Mr.Thread登臺(tái)演出:17
Mr.Thread登臺(tái)演出:18
Mr.Thread登臺(tái)演出:19
Mr.Thread登臺(tái)演出:20
Mr.Thread登臺(tái)演出:21
Mr.Thread登臺(tái)演出:22
Mr.Thread登臺(tái)演出:23
Mr.Thread登臺(tái)演出:24
Mr.Thread登臺(tái)演出:25
Mr.Thread登臺(tái)演出:26
Mr.Thread登臺(tái)演出:27
Mr.Thread登臺(tái)演出:28
Mr.Thread登臺(tái)演出:29
Mr.Thread登臺(tái)演出:30
Mr.Thread登臺(tái)演出:31
Mr.Thread登臺(tái)演出:32
Mr.Thread登臺(tái)演出:33
Mr.Thread登臺(tái)演出:34
Mr.Thread登臺(tái)演出:35
Mr.Thread登臺(tái)演出:36
Mr.Thread登臺(tái)演出:37
Mr.Thread登臺(tái)演出:38
Mr.Thread登臺(tái)演出:39
Mr.Thread登臺(tái)演出:40
Mr.Thread登臺(tái)演出:41
Mr.Thread登臺(tái)演出:42
Mr.Thread登臺(tái)演出:43
Mr.Thread登臺(tái)演出:44
Mr.Thread登臺(tái)演出:45
Mr.Thread登臺(tái)演出:46
Mr.Thread登臺(tái)演出:47
Mr.Thread登臺(tái)演出:48
Mr.Thread登臺(tái)演出:49
Mr.Thread登臺(tái)演出:50
Mr.Thread登臺(tái)演出:51
Mr.Thread登臺(tái)演出:52
Mr.Thread登臺(tái)演出:53
Mr.Thread登臺(tái)演出:54
Mr.Thread登臺(tái)演出:55
Mr.Thread登臺(tái)演出:56
Mr.Thread登臺(tái)演出:57
Mr.Thread登臺(tái)演出:58
Mr.Thread登臺(tái)演出:59
Mr.Thread登臺(tái)演出:60
Mr.Thread登臺(tái)演出:61
Mr.Thread登臺(tái)演出:62
Mr.Thread登臺(tái)演出:63
Mr.Thread登臺(tái)演出:64
Mr.Thread登臺(tái)演出:65
Mr.Thread登臺(tái)演出:66
Mr.Thread登臺(tái)演出:67
Mr.Thread登臺(tái)演出:68
Mr.Thread登臺(tái)演出:69
Mr.Thread登臺(tái)演出:70
Mr.Thread登臺(tái)演出:71
Mr.Thread登臺(tái)演出:72
Mr.Thread登臺(tái)演出:73
Mr.Thread登臺(tái)演出:74
Mr.Thread登臺(tái)演出:75
Mr.Thread登臺(tái)演出:76
Mr.Thread登臺(tái)演出:77
Mr.Thread登臺(tái)演出:78
Mr.Thread登臺(tái)演出:79
Mr.Thread登臺(tái)演出:80
Mr.Thread登臺(tái)演出:81
Mr.Thread登臺(tái)演出:82
Mr.Thread登臺(tái)演出:83
Mr.Thread登臺(tái)演出:84
Mr.Thread登臺(tái)演出:85
Mr.Thread登臺(tái)演出:86
Mr.Thread登臺(tái)演出:87
Mr.Thread登臺(tái)演出:88
Mr.Thread登臺(tái)演出:89
Mr.Thread登臺(tái)演出:90
Mr.Thread登臺(tái)演出:91
Mr.Thread登臺(tái)演出:92
Mr.Thread登臺(tái)演出:93
Mr.Thread登臺(tái)演出:94
Mr.Thread登臺(tái)演出:95
Mr.Thread登臺(tái)演出:96
Mr.Thread登臺(tái)演出:97
Mr.Thread登臺(tái)演出:98
Mr.Thread登臺(tái)演出:99
Mr.Thread登臺(tái)演出:100
Mr.Thread演出結(jié)束了!
2015-04-28
你還沒(méi)理解如何創(chuàng)建線(xiàn)程。
2016-01-07
28行那里要添加一個(gè)Actress的對(duì)象。要不然誰(shuí)知道你執(zhí)行哪個(gè)線(xiàn)程
2015-04-29
第28行代碼不對(duì)吧
2015-04-28
?Thread?actressThread?=?new?Thread(new Actress());改成這樣,你運(yùn)行Runnable方法錯(cuò)了