实验十多线程基础编程阿_第1页
实验十多线程基础编程阿_第2页
实验十多线程基础编程阿_第3页
实验十多线程基础编程阿_第4页
实验十多线程基础编程阿_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验六多线程基础编程实验目的通过继承线程类java.lang.thread创建具有特定功能的线程类,通过实现接口 java.lang.runnable创建可作为线程运行的类,创建线程类对象,启动线程,并观察运行、停 止。创建实现了 runnable接口的线程类对象以后,启动线程,通过观察运行和停止,掌握 线程类java.lang.thread常用方法的使用,掌握对线程执行过程中的界常的处理方法。实验要求编写一个实现接口 java. lang. runnable的简单多线程应用程序。实验内容1. 使川java.lang.thread创建具冇特定功能的线程类;2. 创建以java.lang.run

2、nable为接口的线程类;3. 启动线程,并观察运行、停止。exercise 1#:write a program that displays the name of the thread that executes mainexercise 2#:creat two threads, one thread printand the other print “b" alternately. e.g. abbabaabba. .etc.exercise 3#:随便选择两个城市作为预选旅游冃标。实现两个独立的线程分别显示10次城市名,每 次显示后休眠一段随机时间(1000毫秒以内),哪个

3、城市先显示完毕,就决定去哪个城 市。分别用runnable接口和thread类实现。public class testlvyoupublic static void main(string args)thread tl=new subthread(n北京 j;thread t2=new subthread(”海南”); tl.start();t2.start();class subthread extends thread public subthread(string s) super(s);public void run()for(int i=0;i<10;i+)system.out

4、.print(getname();trysleep(int)(math.random()* 100);catch(intermptedexception e)c.printstacktracc();system, out. printing 去 n+getname();-lolxic d:prograo filesxinox sofivarejcreatorv3lege2001. exe去南南亠鲁南亠星鲁亠鲁亠鲁南亠星鲁亠鲁 wp母母匕母母匕匕毎匕毎匕母母匕匕母匕每 海去去去去去去去去去去去去去去去去去去亠示亠主鲁南亠薔南亠星鲁亠鲁亠鲁南亠星鲁亠工 匕匕每每匕每每匕匕每匕每匕每每匕匕每匕左co

5、ntinue.实验七多线程并发编程实验目的掌握使用关键字synchronized同步方法、使用关键字synchronized同步语句块、和使用 wait> notify (notifyall)实现线程的通信,同吋,练习使用join实现线程的协作。实验要求在实验六的程序棊础上,编写一个多线程并发的应用程序。实验内容1. 创建多个线程;2. 使用synchronized方法实现线程同步;3. 运丿ij wait notify (notifyall)实现线程的通信;4. 使用jom实现线程的协作;exercise 1#:write a program that prints out the e

6、lapsed time each second from the start of execution, with another thread that prints a message every fifteen seconds. have the message-printing thread be notified by the time-printing thread as each second passes by. add another thread that prints a different message every seven seconds without modi

7、fying the time-printing threadexercise 2#:设计两个线程,一个充当电子表,每隔1秒在dos窗口显示下一系统时间;另一个充当 闹钟,每到整点就开始报时,即在dos窗口显示5次整点提示,同时将第一个线程挂起, 报吋完毕再将第一个线程恢复运行。实验步(1) 创建第一个类继承thread类用來显示系统时间(2) 第一个类实现wn()方法创建线程,线程体中每秒获収一次系统时间并显示。(3) 创建笫二个类继承thread类用来实现闹钟功能(4) 第二个类实现run()方法创建线程,线程体中循环判断当前时间是否为整点,若是则 将笫一个线程挂起,并每隔一秒输出提示,提示

8、完毕恢复笫一个线程的执行(5) 创建第三个类,包含main()方法,作为程序的执行入口。import java.util.date;class subthread 1 extends threadpublic subthread 1 (string s)super(s);public void run()date datel=new date();trysleep(int)(math.random()* 100);catch(interruptedexception e)e.printstacktrace();system.out.println(dateltolocalestring();class subthread2 extends threadpublic subthread2(string s)一supcr(s)八一public void run()dae daalunew darep4(sleep(is)(maih.random()*30000);)catch(inoitupodexcepion e)pprinoacktrace?systemoulprintln(dae 1 olocides5.ng()+

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论