




已阅读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.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国宽带路由器行业市场全景分析及前景机遇研判报告
- 2025年全国成人高等学校招生考试(艺术概论-专升本)练习题及答案三
- 山东省无棣第一中学2025届高三上学期第一次月考政治试题(含答案)
- 创新型学习工具在小学英语单元中的有效使用
- 软装设计中的层次感与视觉平衡
- 国家审计优化金融数据治理效率与监管模式
- 学前教育信息化支持与资源共享路径
- 乡土材料在提升乡村景观认同感中的作用
- 新能源汽车专业群与产业需求对接的方式
- 幼儿教师自我反思与专业素养提升的结合路径
- 公路养护技术管理与实施细则
- 2025-2030留学培训行业市场运行态势及发展前景预测与商业合作机会研究报告
- 房地产开发公司工程部经理个人工作总结
- 2025年交通工程师资格考试试题及答案解析
- 2025年私人住宅装修合同及详细工程清单
- 2025年法本法硕真题及答案
- 变压器装配工职业技能考核试卷及答案
- 驻场人员管理协议书8篇
- 2023-2025年中考物理试题分类汇编内能及内能和利用(有解析)
- 秋季传染病健康知识培训课件
- GB/T 46023.2-2025汽车用智能变色玻璃第2部分:聚合物分散液晶调光玻璃
评论
0/150
提交评论