当前位置:文档之家› Java多线程和输入输出流

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;i<10;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;i<10;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");}}}②实验结果截图:第三题:①实验代码: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 IOException{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String str;int array[]=new int[10];for(int i=0;i<10;i++){int a=i+1;System.out.println("请输入第"+a+"整数:");str=br.readLine();array[i]=Integer.parseInt(str);}for(int j=0;j<9;j++){for(int k=j+1;k<10;k++){if(array[k]<array[j]){int temp=array[k];array[k]=array[j];array[j]=temp;}}}for(int m=0;m<10;m++){if(m!=9)System.out.print(array[m]+",");elseSystem.out.print(array[m]);}}}②实验结果截图:第五题:①实验代码:public class Test2 {public static void main(String[] args) {try {System.out.println("请输入内容:");File myfile = new File("save.txt");byte[] outCh = new byte[100];int bytes = System.in.read(outCh, 0, 100);//将文件读入二进制数组中FileOutputStream Fout = new FileOutputStream(myfile);Fout.write(outCh,0,bytes);//将数组中的字节输入到这个流中byte[] inCh = new byte[bytes];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 IOException{ String 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的文件读写机制,练习输入输出流的使用。

相关主题