欢迎来到天天文库
浏览记录
ID:42194065
大小:551.64 KB
页数:20页
时间:2019-09-09
《《java语言程序设计教学资料》java考试java3》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、本科实验报告课程名称:Java语言程序设计实验项目:多线程编程实验地点:致远楼B401专业班级:软件1110学号:2011005485学生姓名:段建生指导教师:韩小红2013年11月26日一、实验目的1・现成的概念、线程的生命周期。2.多线程的编程:扩展Thread类与使用Runnable接口。3.使用多线程机制实现动画二、实验内容(1)运行下面的程序,理解用创建Thread子类的方法实现多线程。//S09_01.Thread.JavaImportjava.util.classS09_01_ThreadextendsThread{intpauseTime;Stringname;
2、publicS09_01_Thread(intx,Stringn){pauseTime=x;name=n;}publicvoidrun(){while(true){try{system.out.println(name+n:"+newDate(System.currentTimeMillis()));Tread.sleep(pauseTime);}catch(Exceptione){System.out.println(e);}staticpublicvoidmain(Stringsrgs[]){S09_01_Threadtpl=newS09_01_Thread(1000,"F
3、ast");tpl.start();S09_01.Threadtp2二newS09_01_Thread(3000,nSlowH);tp2.start();}}(1)运行下面的程序,理解用实现Runnable接口的方法实现多线程。//S09_02_Thread.JavaImportjava.util.*;classS09_02_ThreadimplementsRunnable{intpauseTime;Stringname;publicS09_02_Thread(intx,Stringn){pauseTime=x;name=n;}publicvoidrun(){while(tru
4、e){try{system.out.println(name+H:,'+newDate(System.currentTimeMillis()));Tread.sleep(pauseTime);}catch(Exceptione){System.out.println(e);}staticpublicvoidmain(Stringsrgs[]){Threadtl=newThread(newS09_02_Thread(1000,"Fast”));tl.start();Threadt2=newThread(newS09_02_Thread(3000,*'Slown));t2.star
5、t();}}(1)创建简单的程序ThreeThread.java,该程序将创建三个线程,每个线程应当显示它所运行的时间(可以考虑使用Date类或Calendar类)。三、实验代码3.importjava.text.SimpleDateFormat;importjava,util.*;classAnimalextendsThread{privateSimpleDateFormata;privateStrings;publicAnimal(SimpleDateFormata,Stringstr){this.a=a;s=str;}publicvoidrun(){synchronize
6、d(this,a){System.out.printIn("〃+s);System.out・printin(this・a.format(newDate()));try{sleep((int)(Math,random()*10000));}catch(InterruptedExceptione){}}}}classPoint{publicstaticvoidmain(Stringargs[]){SimpleDateFormatsdf=newSimpleDateFormat(〃yyyy年MM月dd日EEEEEahh时mm分ss秒");newAnimal(sdf,"小猫〃).star
7、t();newAnimal(sdf,"小狗〃).start();newAnimal(sdf,"小鸡〃).start();U1运行结果实验1的运行结果區Problems@Javadoc為声明旦控:Point(13)[Java应用程序]E:javajdkbiSlow:WedNov2017:43:14CST2013Fast:WedNov2017:43:14CST2013Fast:WedNov2017:43:15CST2013Fast:WedNov2017:43:16CST2013Slow:W
此文档下载收益归作者所有