标签:字符串 遍历 字符 image back arch printf magic tst
遍历所有文件,当遍历到文件名是.java结尾的时候,创建一个线程去查找这个文件的内容(是否包含“Magic”字符串),不必等待这个线程结束,继续遍历下一个文件。
1 package multiplethread; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.IOException; 8 9 public class searchFileInDirectory { 10 // 实现Runnable接口方式的多线程 11 public static void funcThread1(String path) { 12 File f = new File(path); 13 if (f.exists()) { 14 File[] fs = f.listFiles(); 15 for (File x : fs) { 16 if (x.isDirectory()) 17 funcThread1(x.getAbsolutePath()); 18 if (x.isFile() && x.getName().contains(".java")) { 19 new Thread(new searchInFile(x)).start(); 20 } 21 } 22 } 23 } 24 25 public static void checkFile(File x) { 26 Thread t = new Thread() { 27 public void run() { 28 try (BufferedReader br = new BufferedReader(new FileReader(x));) { 29 while (true) { 30 String line = br.readLine(); 31 if (line == null) 32 break; 33 if (line.contains("Magic")) { 34 System.out.printf("找到目标字符串\"Magic\",在文件%s%n", x.getAbsoluteFile()); 35 } 36 } 37 } catch (IOException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 } 42 43 }; 44 t.start(); 45 } 46 47 // 匿名类的方式的多线程 48 public static void funcThread2(String path) { 49 File f = new File(path); 50 if (f.exists()) { 51 File[] fs = f.listFiles(); 52 for (int i = 0; i < fs.length; i++) { 53 File x = fs[i]; 54 if (x.isDirectory()) { 55 funcThread2(x.getAbsolutePath()); 56 } 57 if (x.isFile() && x.getName().contains(".java")) { 58 checkFile(x); // 理论上直接写到这里可以,但是为了美观便于阅读,就单独写到checkFile()中 59 } 60 } 61 62 } 63 } 64 65 // 单线程 66 public static void funcNoThread(String path) { 67 File f = new File(path); 68 if (f.exists()) { 69 File[] fs = f.listFiles(); 70 for (File x : fs) { 71 if (x.isDirectory()) 72 funcNoThread(x.getAbsolutePath()); 73 if (x.isFile() && x.getName().contains(".java")) { 74 try (BufferedReader br = new BufferedReader(new FileReader(x));) { 75 while (true) { 76 String line = br.readLine(); 77 if (line == null) 78 break; 79 if (line.contains("Magic")) { 80 System.out.printf("找到目标字符串\"Magic\",在文件%s%n", x.getAbsoluteFile()); 81 } 82 } 83 } catch (IOException e) { 84 // TODO Auto-generated catch block 85 e.printStackTrace(); 86 } 87 88 } 89 } 90 } 91 } 92 93 public static void main(String[] args) { 94 String path = "F:\\project\\javastudy"; 95 long st1 = System.currentTimeMillis(); 96 funcThread1(path); 97 long et1 = System.currentTimeMillis(); 98 System.out.printf("多线程Thread1下花费时间:%d ms", et1 - st1); 99 100 long st2 = System.currentTimeMillis(); 101 funcThread2(path); 102 long et2 = System.currentTimeMillis(); 103 System.out.printf("多线程Thread2下花费时间:%d ms", et2 - st2); 104 105 long st3 = System.currentTimeMillis(); 106 funcNoThread(path); 107 long et3 = System.currentTimeMillis(); 108 System.out.printf("单线程下花费时间:%d ms", et3 - st3); 109 } 110 }
效果图:
...
...
由于文件数量较少,多线程与单线程所耗时间基本一致。
标签:字符串 遍历 字符 image back arch printf magic tst
原文地址:https://www.cnblogs.com/gilgamesh-hjb/p/12233263.html