Created
July 1, 2018 07:57
-
-
Save foreverlms/db73a170b13d3e761ff1ea43e62af288 to your computer and use it in GitHub Desktop.
Java BblockingQueue和synchronized加锁关键字两种方法实现递归搜索文件关键字 用于衡量两种方法执行效率不准确,因为存在多线程,导致得出的时间不是正确的。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter14.blockingQueue; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Scanner; | |
import java.util.concurrent.ArrayBlockingQueue; | |
import java.util.concurrent.BlockingQueue; | |
public class BlockingQueueTest { | |
private static final int FILE_QUEUE_SIZE = 10; | |
private static final int SEARCH_THREADS = 100; | |
private static final File DUMMY = new File(""); | |
private static BlockingQueue<File> queue = new ArrayBlockingQueue<>(FILE_QUEUE_SIZE); | |
public static void main(String[] args) { | |
long startTime = System.currentTimeMillis(); | |
try (Scanner in = new Scanner(System.in)){ | |
System.out.print("Enter base directory (e.g. /opt/jdk1.8.0/src): "); | |
String dir = in.nextLine(); | |
System.out.print("Enter keyword: "); | |
String key = in.nextLine(); | |
Runnable enumerator = () -> { | |
try { | |
enumerate(new File(dir)); | |
queue.put(DUMMY); | |
}catch (InterruptedException e){ | |
} | |
}; | |
new Thread(enumerator).start(); | |
for(int i=1 ; i <= SEARCH_THREADS; i++){ | |
Runnable searcher = () -> { | |
try{ | |
boolean done = false; | |
while (!done){ | |
File file = queue.take(); | |
if (file == DUMMY){ | |
queue.put(file); | |
done = true; | |
}else search(file,key); | |
} | |
}catch (IOException e){ | |
} | |
catch (InterruptedException e){ | |
} | |
}; | |
new Thread(searcher).start(); | |
} | |
} | |
long endTime = System.currentTimeMillis(); | |
System.out.println("耗时:"+(endTime-startTime)); | |
} | |
private static void search(File file, String key) throws IOException { | |
try(Scanner in = new Scanner(file,"UTF-8")){ | |
int lineNumber = 0 ; | |
while (in.hasNextLine()){ | |
lineNumber++; | |
String line = in.nextLine(); | |
if (line.contains(key)) System.out.printf("%s:%d:%s:%n",file.getPath(),lineNumber,line); | |
} | |
} | |
} | |
private static void enumerate(File dir) throws InterruptedException { | |
File[] files = dir.listFiles(); | |
for (File file : files){ | |
if (file.isDirectory()) enumerate(file); | |
else queue.put(file); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter14.blockingQueue; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
public class LockTest { | |
private static final int SEARCH_THREADS = 100; | |
private static final File DUMMY = new File(""); | |
private static List<File> queue = new ArrayList<>(); | |
public static void main(String[] args) { | |
long startTime = System.currentTimeMillis(); | |
try (Scanner in = new Scanner(System.in)) { | |
System.out.print("Enter base directory (e.g. /opt/jdk1.8.0/src): "); | |
String dir = in.nextLine(); | |
System.out.print("Enter keyword: "); | |
String key = in.nextLine(); | |
try { | |
enumerate(new File(dir)); | |
queue.add(DUMMY); | |
}catch (InterruptedException e){ | |
} | |
final int[] count ={0}; | |
for (int i = 1; i<= SEARCH_THREADS; i++){ | |
Runnable searcher = () -> { | |
while (true){ | |
synchronized (count){ | |
boolean done = count[0] == queue.size(); | |
if (done){ | |
break; | |
}else{ | |
try { | |
search(queue.get(count[0]), key); | |
}catch (IOException e){ | |
} | |
count[0]++; | |
} | |
} | |
} | |
}; | |
new Thread(searcher).start(); | |
} | |
} | |
long endTime = System.currentTimeMillis(); | |
System.out.println("耗时:"+(endTime-startTime)); | |
} | |
private static void enumerate(File dir) throws InterruptedException { | |
File[] files = dir.listFiles(); | |
for (File file : files){ | |
if (file.isDirectory()) enumerate(file); | |
else queue.add(file); | |
} | |
} | |
private static void search(File file, String key) throws IOException { | |
try(Scanner in = new Scanner(file,"UTF-8")){ | |
int lineNumber = 0 ; | |
while (in.hasNextLine()){ | |
lineNumber++; | |
String line = in.nextLine(); | |
if (line.contains(key)) System.out.printf("%s:%d:%s:%n",file.getPath(),lineNumber,line); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment