Java多线程和输入输出流

Java多线程和输入输出流

ID:37845199

大小:135.54 KB

页数:9页

时间:2019-06-01

Java多线程和输入输出流_第1页
Java多线程和输入输出流_第2页
Java多线程和输入输出流_第3页
Java多线程和输入输出流_第4页
Java多线程和输入输出流_第5页
资源描述:

《Java多线程和输入输出流》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、班级:13科技2班学号:201324131225姓名:许耿宁Java多线程和输入输出流一、实验目的:1.熟悉利用Thread类建立多线程方法。2.熟悉利用Thread接口建立多线程方法。3.熟悉Java的文件读写机制,练习输入输出流的使用。二、实验内容:1.阅读下列程序,分析并上机检验其功能。publicclassDelayRunnableimplementsRunnable{privatestaticintcount=0;privateintno;privateintdelay;publicDelayRunnable(){count++;no=count;}publicvoidrun()

2、{try{for(inti=0;i<10;i++){delay=(int)(Math.random()*5000);Thread.sleep(delay);System.out.println("Thread"+no+"withadelay"+delay);}}catch(InterruptedExceptione){}}}classMyRunnable{publicstaticvoidmain(Stringargs[]){DelayRunnabler1=newDelayRunnable();9DelayRunnabler2=newDelayRunnable();Threadthread1

3、=newThread(r1);Threadthread2=newThread(r2);thread1.start();thread2.start();try{Thread.sleep(1000);}catch(InterruptedExceptione){System.out.println("Threadwrong");}}}2.将上列程序利用Runnable接口改写,并上机检验。3.创建简单的程序ThreeThread.java,该程序将创建三个线程,每个线程应当显示它所运行的时间(可以考虑使用Date类或Calendar类)。4.键盘输入10个整数,从小到大进行排序。5.接收键盘输入

4、的字符串,用FileInputStream类将字符串写入文件,用FileOutputStream类读出文件内容显示在屏幕上。6.将一个文本文件的内容按行读出,每读出一行就顺序加上行号,并写入到另一个文件中。三、实验要求:1.通过实验掌握Thread、Runnable使用方法;2.程序必须能够实现多线程;3.程序必须能够完成题目要求;4.通过实验掌握文件输入输出流的使用方法;5.程序必须能够从键盘接收字符串并保存在文件中;6.程序必须能够读出文件内容显示在屏幕上;7.写出实验报告。四、实验代码及截图:第一题:在编译器上运行程序得到截图所示结果:9第二题:①实验代码publicclassDel

5、ayThreadextendsThread{privatestaticintcount=0;privateintno;privateintdelay;publicDelayThread(){count++;no=count;}publicvoidrun(){try{for(inti=0;i<10;i++){delay=(int)(Math.random()*5000);sleep(delay);System.out.println("Thread"+no+"withadelay"+delay);}}catch(InterruptedExceptione){}}}9classMyThread

6、{publicstaticvoidmain(Stringargs[]){DelayThreadthread1=newDelayThread();DelayThreadthread2=newDelayThread();thread1.start();thread2.start();try{Thread.sleep(1000);}catch(InterruptedExceptione){System.out.println("Threadwrong");}}}②实验结果截图:第三题:①实验代码:publicclassMultiThread{staticLockmylock;publicstat

7、icvoidmain(String[]args){RunningObject1r1=newRunningObject1();9Threadt1=newThread(r1,"t1");Threadt2=newThread(r1,"t2");Threadt3=newThread(r1,"t3");t1.start();t3.start();t2.start();}staticclassRunningObject1implem

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。