




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
. . . . 班级:13科技2班 学号:201324131225 姓名:许耿宁 Java多线程和输入输出流一、实验目的:1.熟悉利用Thread类建立多线程方法。2.熟悉利用Thread接口建立多线程方法。3.熟悉Java的文件读写机制,练习输入输出流的使用。二、实验内容: 1.阅读下列程序,分析并上机检验其功能。 public class DelayRunnable implements Runnableprivate static int count=0;private int no;private int delay;public DelayRunnable() count+; no=count; public void run() try for (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 DelayRunnable();Thread thread1=new Thread(r1);Thread thread2=new Thread(r2);thread1.start();thread2.start();tryThread.sleep(1000);catch(InterruptedException e)System.out.println(Thread wrong);2.将上列程序利用Runnable接口改写,并上机检验。3.创建简单的程序ThreeThread.java,该程序将创建三个线程,每个线程应当显示它所运行的时间(可以考虑使用Date类或Calendar类)。4.键盘输入10个整数,从小到大进行排序。5.接收键盘输入的字符串,用FileInputStream类将字符串写入文件,用FileOutputStream类读出文件内容显示在屏幕上。6.将一个文本文件的内容按行读出,每读出一行就顺序加上行号,并写入到另一个文件中。三、实验要求:1.通过实验掌握Thread 、Runnable使用方法;2.程序必须能够实现多线程;3.程序必须能够完成题目要求;4.通过实验掌握文件输入输出流的使用方法;5.程序必须能够从键盘接收字符串并保存在文件中;6.程序必须能够读出文件内容显示在屏幕上;7.写出实验报告。四、实验代码及截图:第一题:在编译器上运行程序得到截图所示结果:第二题:实验代码public class DelayThread extends Thread private static int count=0; private int no; private int delay; public DelayThread() count+; no=count; public void run() try for (int i=0;i10;i+) delay=(int)(Math.random()*5000); sleep(delay); System.out.println(Thread +no+ with a delay +delay); catch(InterruptedException e) class MyThread public static void main(String args) DelayThread thread1=new DelayThread(); DelayThread thread2=new DelayThread(); thread1.start(); thread2.start(); tryThread.sleep(1000);catch(InterruptedException e)System.out.println(Thread wrong); 实验结果截图:第三题:实验代码:public class MultiThread static Lock mylock; public static void main(String args) RunningObject1 r1 = new RunningObject1(); Thread t1 = new Thread(r1, t1); Thread t2 = new Thread(r1, t2); Thread t3 = new Thread(r1, t3); t1.start(); t3.start(); t2.start(); static class RunningObject1 implements Runnable public void run() synchronized(this) String name=Thread.currentThread().getName(); System.out.println(name+开始时间:+Calendar.getInstance().getTimeInMillis(); for (int i = 0; i 100000000; i+) if (i = 9999999) System.out.println(name+结束时间:+Calendar.getInstance().getTimeInMillis(); break;实验结果截图:第四题:实验代码:public class Test1 public static void main(String args) throws IOExceptionBufferedReader br=new BufferedReader(new InputStreamReader(System.in);String str;int array=new int10;for(int i=0;i10;i+)int a=i+1;System.out.println(请输入第+a+整数:);str=br.readLine();arrayi=Integer.parseInt(str);for(int j=0;j9;j+)for(int k=j+1;k10;k+)if(arraykarrayj)int temp=arrayk; arrayk=arrayj; arrayj=temp;for(int m=0;m10;m+)if(m!=9)System.out.print(arraym+,);elseSystem.out.print(arraym);实验结果截图:第五题:实验代码:public class Test2 public static void main(String args) try System.out.println(请输入内容:); File myfile = new File(save.txt); byte outCh = new byte100; int bytes = System.in.read(outCh, 0, 100);/将文件读入二进制数组中 FileOutputStream Fout = new FileOutputStream(myfile); Fout.write(outCh,0,bytes);/将数组中的字节输入到这个流中 byte inCh = new bytebytes; FileInputStream Fin = new FileInputStream(myfile); Fin.read(inCh); System.out.println(读取文件内容:); System.out.println(new String(inCh); catch (IOException e) System.out.println(e.toString(); 实验结果截图:第六题:实验代码:public class Test3 public static void main(String args) throws IOExceptionString str=null;FileReader file=new FileReader(F:/1.txt);FileWriter b=new FileWriter(F:/2.txt);BufferedWriter c=new BufferedWriter(b);LineNumberReader in=new LineNumberReader(file);while(str=in.readLine()!=null)System.out.println(in.getLineNumber()+:+str);c.write(str);c.newLine();c.close();in.close();实验结果截图:五、总结 这次实验不难,参考书上的内容就可以很容易的做出来了。通过这次实验,我熟悉了利用Thread类建立多线程方法;熟悉了利用Thread接口建立多线程方法;熟悉了Java的文件读写机制,练习输入输出流的使用。欢迎您的光临,Word文档下载后可修改编辑.双击可删除页眉页脚.谢谢!希望您提出您宝贵的意见,你的意见是我
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 传统草药养生法案
- 2025云南省文山州富宁县城区学校教师交流轮岗(99人)笔试备考试题及答案解析
- 2025云南丽江宁蒗彝族自治县应急管理局面向社会招聘公益性岗位1人笔试备考题库及答案解析
- 烤箱维修手册
- 2025西安未央区辛家庙社区卫生服务中心招聘(8人)笔试备考试题及答案解析
- 2025天津市北辰区教育系统第二次招聘教师22人笔试含答案
- 2025年天津公安基层人民警察考试(申论)历年参考题库含答案详解
- 入侵报警监控预案
- 2025年心血管内科心脏彩超检查技能考察答案及解析
- 2025年事业单位笔试-广东-广东助产护理(医疗招聘)历年参考题库含答案解析
- 环境灾害预警系统-洞察及研究
- 广播电视机线员技能操作考核试卷及答案
- 无人机基地技术支持与服务方案
- 水果代销合同(标准版)
- 辅警结构化面试题及答案(2025年)
- 2025年托管厨师合同协议书
- 2025年医疗器械专业知识培训试题及答案
- 肠结核合并肠穿孔护理查房
- 矛盾纠纷调解课件
- 《我是什么》课件 统编版语文二年级上册
- 师生互动与课件使用
评论
0/150
提交评论