




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1班级:13科技2班 学号:201324131225:许耿宁Java多线程和输入输出流一、实验目的:1.熟悉利用 Thread 类建立多线程方法。2.熟悉利用 Thread 接口建立多线程方法。3.熟悉 Java 的文件读写机制,练习输入输出流的使用。二、实验容:1.阅读下列程序,分析并上机检验其功能。 public class DelayRunnableimplements Runnable private static int count=0;private int no;private int delay;public DelayRunnable()count+; no=count;pu
2、blic void run()tryfor (int i=0;i10;i+) delay=(int)(Math.random()*5000);Thread.sleep(delay);System.out.println(Thread +no+ with a delay +delay); catch(InterruptedException e)class MyRunnablepublic static void main(String args) DelayRunnable r1 = new DelayRunnable();DelayRunnable r2 = new DelayRunnabl
3、e();Thread thread1=new Thread(r1);Thread thread2=new Thread(r2);thread1.start();2thread2.start();tryThread.sleep(1000);catch(InterruptedException e)System.out.println(Thread wrong);2. 将上列程序利用 Runnable 接口改写,并上机检验。3. 创建简单的程序 ThreeThread.java ,该程序将创建三个线程, 每个线程应 当显示它所运行的时间(可以考虑使用 Date 类或 Calendar 类)。4.
4、键盘输入 10 个整数,从小到大进行排序。5. 接收键盘输入的字符串,用 FileInputStream 类将字符串写入文件,用FileOutputStream 类读出文件容显示在屏幕上。6. 将一个文本文件的容按行读出,每读出一行就顺序加上行号,并写入到另一个文件中。三、实验要求:1. 通过实验掌握 Thread 、 Runnable 使用方法;2. 程序必须能够实现多线程;3. 程序必须能够完成题目要求;4. 通过实验掌握文件输入输出流的使用方法;5. 程序必须能够从键盘接收字符串并保存在文件中;6. 程序必须能够读出文件容显示在屏幕上;7. 写出实验报告。四、实验代码及截图: 第一题:
5、在编译器上运行程序得到截图所示结果:3terminMyRunnable Java ApplieationJ D:Js;Thread1 with a delayThread2wtha delayThread2with白delayThread1witha delayThread2 witha delayThread1witha delayThre白d 2witha del白yThread1withdLdelayThread2witha delayThread1witha delayThread2 with a delayThread2with a delayThread2 with a delyT
6、hread1witha delayThread1witha delayThread2witha delayThread1witha delayThread2withdelayThread1with a delayThread1witha delay32873870353148724491416461132684200220811015851681274312303400236023984810298第二题:实验代码public class DelayThread exte nds Threadprivate static int coun t=0;private int no;private
7、int delay;public DelayThread()coun t+;no=co unt;public void run()tryfor (i nt i=0;i10;i+)delay=(i nt)(Math.ra ndom()*5000);sleep(delay);System.out.pri ntl n(Thread +no+ with a delay+delay); catch(I nterruptedExcepti on e)4class MyThreadpublic static void main( Stri ng args) DelayThread thread 仁 new
8、DelayThread();DelayThread thread2=new DelayThread(); thread1.start();thread2.start();tryThread.sleep(1000);catch(l nterruptedExcepti on e) System.out.pri ntl n(Thread wron g);实验结果截图: MyTliread Java Applkaticn D:JavajThread 1 with adelay720Thread 2评ith a delay2041Thread 1 with adelay2560Thread 2 with
9、 adelay 4080Thread 1 with adelay3169Thread 1 with adelay490Thread 2 with adelay960Thread 1 with adelay 960Thread 2 with adelay1179Thread 1 with adelay 410Thread 2 with adelay80 CThread 1 with adelay3415Thread 2叩ith a delay4053Thread 1 with adely3159Thread 2 with adelay2476Thread 1 with日delay1361Thre
10、ad 2 with adelay3926Thread 1 with adelay2845Thread 2 with adelay1052Thread 2 with adelay 422第三题:实验代码:public class MultiThread static Lock mylock;public static void main( Stri ng args) Runnin gObject1 r1 = new Runnin gObject1();Thread t1 = new Thread(r1, t1);Thread t2 = new Thread(r1, t2);Thread t3 =
11、 new Thread(r1, t3);t1.start();t3.start();5t2.start();static class RunningObjectl implements Runnable public void run() syn chro nized(this)Stri ng n ame=Thread.curre ntThread().getName();System.out.pri ntln(n ame+开始日寸间:+Cale ndar.getl nsta nce().getTimel nMillis();for (int i = 0; i MultrThread JJav
12、a Applicdtion DiJavdjreL9.0_9rbintl幵始时I环1466407635147=结束时间4466407635200t2ff始时间:L46640763520014664076352211466407635222结莉寸间:1466407635227第四题:实验代码:public class Test1 public static void main(String args) throws IOExceptionBufferedReaderbr=newBufferedReader( newIn putStreamReader(System.i n);Stri ng str
13、;int array=new in t10;246for(int i=0;i10;i+)int a=i+1;System.out.println(请输入第+a+整数:);str=br.readL in e();arrayi=l nteger.parsel nt(str);for(i nt j=0;j9;j+)for(int k=j+1;k10;k+)if(arraykarrayj)int temp=arrayk;arrayk=arrayj; arrayj=temp;for(i nt m=0;m10;m+)if(m!=9) System.out.pri nt(arraym+,);elseSyst
14、em.out.pri nt(arraym);实验结果截图:termi n dte-destl刖Appiic肿on U;y| rel,b inj vaJ.Vi口I请谕入第l整情谕入第2整数:请输入第3整数:请输入第4整数:34请谕入第5整数:请输入第6整数:请输入第7龜数:66请输入第g整数:99请输入第9整数:744请输入策10整数:201,13,20,22,24,34,44,53,6r99第五题:实验代码: public class Test2 public static void main( Stri ng args) try System.out.println(”请输入容:);File
15、 myfile = new File(save.txt);byte outCh = new byte100;int bytes = System.i n.read(outCh, 0, 100);/将 文件读入二进制数组中FileOutputStream Fout = new FileOutputStream(myfile);Fout.write(outCh,0,bytes);/将数组中的字节输入到这个流中 byte inCh= new bytebytes;File In putStream Fin = new File In putStream(myfile);Fin. read(i nCh)
16、;System.out.pri ntln(读取文件容:);System.out.pri ntln(n ew Strin g(i nCh); catch (IOExcepti on e) System.out.pri ntl n( e.toStri ng();实验结果截图:utermi门曰tedn T皿2 (1 Java Application D:Jdvajre 1.3.1 binjavaw.exe 20166冃20日 请输入内容:好好学习,天天向上快取文件内容;好好学习,天天问上文件(F)務任格式 grt(V)亀旬(H好好学习,天天向上第六题:实验代码:public class Test3
17、public static void main(String args) throws lOException Stri ng str=n ull;FileReader file=new FileReader(F:/1.txt);8FileWriter b=new FileWriter(F:/2.txt); BufferedWriter c=newBufferedWriter(b);Lin eNumberReader in=new Lin eNumberReader(file); while(str=in.readLi ne()!=null)System.out.pri ntl n(i n.getLi neNumber()+:+str); c.write(st
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 新冠肺炎疫苗外溢应急预案(3篇)
- 各种应急预案的特点是(3篇)
- 池州职业技术学院《地下水污染与防治》2024-2025学年第一学期期末试卷
- 专项施工方案需要应急预案(3篇)
- 项目管理中的专项应急预案(3篇)
- 汕头市房屋安全应急预案(3篇)
- 建设施工安全事件应急预案(3篇)
- 天津医学高等专科学校《中小学心理健康教育指导纲要解读》2024-2025学年第一学期期末试卷
- 大连医科大学《清洁生产与实践》2024-2025学年第一学期期末试卷
- 湖南生物机电职业技术学院《数字音频编辑与处理》2024-2025学年第一学期期末试卷
- 医院安保课件
- 文印员考试题库及答案
- 2025年涂料行业研究报告及未来发展趋势预测
- 2025年部编版新教材语文九年级上册教学计划(含进度表)
- 2025河北唐山某国有企业单位招聘劳务派遣工作人员44人笔试参考题库附带答案详解(10套)
- 班主任与家长沟通课件
- 2025年安庆怀宁县事业单位招聘考试试题【答案】
- 预防残疾知识讲座课件
- 浪漫七夕领证活动方案
- 大型活动策划组织与执行合同
- 培训机构教师如何做续费转介绍
评论
0/150
提交评论