你的位置:首页 > Java教程

[Java教程]Java 多线程之龟兔赛跑(文件夹——读取文件——时间)


 

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

 

描述:

乌龟和兔子(各自是一个Java线程)在我们的电脑上赛跑,我们为它们指定一个跑道(本地文件系统上的一个目录,该目录包含子目录)。跑的规则是读“跑道”上的所有文件。兔子很聪明,只读文件的元信息(路径名、大小、最后修改时间),但每读完一个文件就要睡1秒钟;乌龟很笨,读文件的每一个字节(乌龟每次读取数据的大小不超过1024字节),但是它不停歇地读,直到读完所有文件。写程序模拟它们同时起跑的比赛过程,最后输出“兔子赢!”或“乌龟赢!”

 

 1 import java.io.File; 2  3 /**兔子线程 4  * 路径名 大小 最后修改时间 读完一个文件sleep。。 5  * @author gang 6  * 7 */ 8 public class Rabbit implements Runnable{ 9   File file;10   public Rabbit(){11     file = null;12   }13   public Rabbit(File file){14     this.file = file;15   }16   public void run(){17     read(file);18   }19   20   public void read(File file){21     if(file.isDirectory()){22       for(File files:file.listFiles())23         read(files);24     } else {25       file.getAbsolutePath();26       file.lastModified();27       file.length();28       try {29         Thread.sleep(1000);30       } catch (InterruptedException e) {31         // TODO Auto-generated catch block32         e.printStackTrace();33       }34     }35   }36 }

 1 package Thread; 2  3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7  8 /**乌龟线程 9  * 每次读取不超过1024字节 一直读完整个文件夹10  * @author gang11  *12 */13 public class Turtle implements Runnable{14   File file;15   public Turtle(){16     file = null;17   }18   public Turtle(File file){19     this.file=file;20   }21   public void run() {22     // TODO Auto-generated method stub23     try {24       read(file);25     } catch (IOException e) {26       // TODO Auto-generated catch block27       e.printStackTrace();28     }29   }30   31   public void read(File file) throws IOException{32     if(file.isDirectory()){33       for(File files:file.listFiles())34         read(files);35     } else{36       FileInputStream fiStream = new FileInputStream(file);37       byte b[] = new byte[1024];38       int i;39       while((i = fiStream.read(b)) != -1);40     }41   }42 }

 1 package Thread; 2  3 import java.io.File; 4 import java.util.Scanner; 5  6 /**龟兔线程测试 输入相应的文件夹  7  *  8  * @author gang 9  *10 */11 public class MainThread {12   public static void main(String[] args){13     String pathStr;      14     Scanner in = new Scanner(System.in); 15     pathStr = in.nextLine();      // input pathFile16     File path = new File(pathStr);   // path17     18     Rabbit rabbit = new Rabbit(path);19     Turtle turtle = new Turtle(path);20     21     long t1Begin = System.currentTimeMillis();  //start22     Thread t1 = new Thread(rabbit);23     t1.start();24     while(t1.isAlive()); 25     long t1End = System.currentTimeMillis();  // t1 end26     27     long t2Begin = System.currentTimeMillis();  //start28     Thread t2 = new Thread(turtle);29     t2.start();30     while(t2.isAlive());31     long t2End = System.currentTimeMillis();  // t2 end32     33     // 测试结果34     if((t1End-t1Begin) > (t2End-t2End)){35       System.out.printf("time of Rabbit is %d ms.\n",(t1End-t1Begin));36       System.out.printf("time of Turtle is %d ms.\n",(t2End-t2Begin));37       System.out.println("Turtle win the game.");38     } else if((t1End-t1Begin) < (t2End-t2End)){39       System.out.printf("time of Rabbit is %d ms.\n",(t1End-t1Begin));40       System.out.printf("time of Turtle is %d ms.\n",(t2End-t2Begin));41       System.out.println("Rabbit win the game.");42     } else{43       System.out.printf("time of Rabbit is %d ms.\n",(t1End-t1Begin));44       System.out.printf("time of Turtle is %d ms.\n",(t2End-t2Begin));45       System.out.println("nobody win the game.");46     }47   } 48 }

 

代码解释见上注释。

输入相应测试的文件夹,即可开始测试,若文件项目多,则运行时间较长,请耐心等待。