(参考答案版)福建农林大学_java_实验七_多线程
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java面向对象程序设计
实验七
多线程
学号:
姓名:
学院:计算机与信息学院
专业班级:
指导老师:林志伟
2 015.12.3
1 .单线程实例的运行结果是:
mainbegins12345678910111213141516171819202122 2 324252627282930313233343536373839404142434445 647484950ABCDEFGHIJKLMNOPQRSTUVWXYZa 4
bcdefghijklmnopqrstuvwxyzmainends
多线程视力的运行结果是:
mainbeginsmainendsA1B2C3D4E5F6G7H8I9JKLMNO
PQRS 10 T11U 12 V13 W 14X15 Y16Z 17 a18b 19 c20 d 21e22 f 23 g24 h25i 26j 27k 28l 29m30n31o32p33q34r35 s36 t 37u38v39w40x41y42z4344454647484950
多线程与单线程的区别在于多线程可以同时运行多段代码,提高程序的执行
效率。 2 .程序运行结果如下:
mainprint0 Thread1print5 Thread1print6 Thread1print7 Thread1print8 Thread1print9 Thread2print0 Thread2print1 Thread2print2 Thread2print3 Thread2print4 Thread2print5 Thread2print6 Thread2print7 Thread2print8 Thread2print9
mainprint1 mainprint2 mainprint3 mainprint4 mainprint5 mainprint6 mainprint7 mainprint8 mainprint9 Thread1print0 Thread1print1 Thread1print2 Thread1print3 Thread1print4
Threads 类实现了Runnable 接口,故程序运行到了t1.start()时会创建一个新
的线程并运行t1.run()内部的代码,运行到t2.start()时会再创建一个新的线程, 并运行t2.run()内部的代码。虽然这个程序是多线程的,但是由于cpu 运行速 度过快,导致这个程序的运行结果跟单线程运行的结果没有区别。当将run() 方法中的循环次数增加到了100 时,就可以看出多线程运行结果与单线程运 行结果的不同了。
循环次数增加后的程序运行结果如下:
Thread1print4 Thread1print5 Thread2print24 Thread1print6 Thread2print25 Thread1print7 Thread2print26 Thread1print8 Thread2print27 Thread1print9 Thread2 print28 Thread1print10 Thread2print29 Thread1print11 Thread2print30 Thread1print12 Thread2print31 Thread1print13 Thread2print32 Thread1print14 Thread2print33 Thread1print15 Thread2print34 Thread1print16 Thread2print35 Thread1print17 Thread2print36 Thread1print18 Thread2print37 Thread1print19 Thread2print38 Thread1print20 Thread2print39 Thread1print21 Thread2print40 Thread1print22 Thread2print41
Thread1print23
Thread2print42
Thread1print24
Thread2print43
Thread1print25
Thread2print44
Thread1print26
Thread2print45
Thread1print27
Thread2print46
Thread1print28
Thread2print47
Thread1print29
Thread2print48
Thread1print30
Thread2print49
Thread1print31
Thread2print50
Thread1print32
Thread2print51
Thread1print33
Thread2print52
Thread1print34
Thread2print53
Thread1print35
Thread2print54
Thread1print36
Thread2print55
Thread2print56
Thread2print57
Thread2print58
Thread2print59
Thread2print60
Thread1print37
Thread2print61
Thread1print38 Thread2print84
Thread1print54
Thread2print85
Thread1print55
Thread2print86
Thread2print87
Thread1print56
Thread2print88
Thread1print57
Thread2print89
Thread1print58
Thread2print90
Thread1print59
Thread2print91
Thread1print60
Thread2print92
Thread1print61
Thread2print93
Thread1print62
Thread2print94
Thread1print63
Thread2print95
Thread1print64
Thread2print96
Thread1print65
Thread2print97
Thread1print66
Thread2print98
Thread1 print67
Thread2print99
Thread1print68
Thread1print69
Thread1print70
Thread1print71
Thread1print72
Thread1print73
Thread1print74
Thread1print75
Thread1print76
Thread1print77
Thread1print78
Thread1print79
Thread1print80
Thread1print81
Thread1print82
Thread1print83
Thread1print84
Thread1print85
Thread1print86
Thread1print87
Thread1print88
Thread1print89
Thread1print90
Thread1print91
Thread1print92
Thread1print93
Thread1print94
Thread1print95
Thread1print96
Thread1print97
Thread1print98
Thread1print99
Thread2print62
Thread1print39
Thread2print63
Thread1print40
Thread2print64
Thread1print41
Thread2print65
Thread1print42
Thread2print66
Thread1print43
Thread2print67
Thread1print44
Thread2print68
Thread1print45
Thread2print69
Thread1print46
Thread2print70
Thread2print71
Thread2print72
Thread2print73
Thread2print74
Thread2print75
Thread2print76
Thread2print77
Thread1print47
Thread2print78
Thread1print48
Thread2print79
Thread1print49
Thread2print80
Thread1print50
Thread2print81
Thread1print51
Thread2print82
Thread1print52
3 .程序源代码如下:
packageexperiment.seven;
classWriteWordThreadextendsThread{
WriteWordThread(Strings){