- 1、本文档共9页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Java多线程和输入输出流创新
班级:13科技2班 学号:201324131225 姓名:许耿宁
Java多线程和输入输出流
一、实验目的:
1.熟悉利用Thread类建立多线程方法。
2.熟悉利用Thread接口建立多线程方法。
3.熟悉Java的文件读写机制,练习输入输出流的使用。
二、实验内容:
1.阅读下列程序,分析并上机检验其功能。
public class DelayRunnable implements Runnable{
private 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 MyRunnable{
public 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();
try{
Thread.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();
try{
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println(Thread wrong);
}
}
}
②实验结果
文档评论(0)