Created
November 27, 2016 13:57
-
-
Save junwen12221/3ada4509eac30f524a804cd57dac0f86 to your computer and use it in GitHub Desktop.
This file contains 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 seven.ne2; | |
import seven.DocumentTask; | |
import seven.ForkJoinSearch; | |
import seven.SearchResult; | |
import java.io.File; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.List; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.ForkJoinPool; | |
import java.util.concurrent.TimeUnit; | |
import java.util.stream.Collectors; | |
/** | |
* Created by Administrator on 2016/11/25 0025. | |
*/ | |
public class Main { | |
/* public static void main(String[] args) throws Exception { | |
SynchronousQueue<String> queue = new SynchronousQueue<>(); | |
Thread thread = new Thread(() -> { | |
try { | |
System.out.println(queue.take()); | |
} catch (InterruptedException e) { | |
e.fillInStackTrace(); | |
} | |
}); | |
thread.start(); | |
Thread.sleep(1000L); | |
if (queue.offer("S1")) { | |
System.out.println("scucess"); | |
} else { | |
System.out.println("faild"); | |
} | |
}*/ | |
public static void printFile(File f, int len) { | |
File file[] = f.listFiles(); | |
for (int i = 0; i < file.length; i++) { | |
// System.out.println(str + file[i].getName()); | |
if (file[i].isDirectory()) { | |
printFile(file[i], len + 1); | |
} | |
} | |
} | |
static class FileContent { | |
Path path; | |
List<String> stringList; | |
public FileContent(Path path, List<String> stringList) { | |
this.path = path; | |
this.stringList = stringList; | |
} | |
public Path getPath() { | |
return path; | |
} | |
public void setPath(Path path) { | |
this.path = path; | |
} | |
public List<String> getStringList() { | |
return stringList; | |
} | |
public void setStringList(List<String> stringList) { | |
this.stringList = stringList; | |
} | |
} | |
/** | |
* Main method of the class | |
*/ | |
public static void main(String[] args) throws Exception { | |
String str = "D:\\homework\\src\\main"; | |
ForkJoinSearch forkJoinSearch = (String path) -> { | |
List<Path> pathList; | |
try { | |
pathList = Files | |
.walk(Paths.get(path), 4) | |
.filter((path1 -> !Files.isDirectory(path1))) | |
.collect(Collectors.toList()); | |
// Create a ForkJoinPool | |
ForkJoinPool pool = new ForkJoinPool(); | |
DocumentTask documentTask = new DocumentTask(pathList, 0, pathList.size(), "main"); | |
pool.execute(documentTask); | |
// Write statistics about the pool | |
do { | |
System.out.printf("******************************************\n"); | |
System.out.printf("Main: Parallelism: %d\n", pool.getParallelism()); | |
System.out.printf("Main: Active Threads: %d\n", pool.getActiveThreadCount()); | |
System.out.printf("Main: Task Count: %d\n", pool.getQueuedTaskCount()); | |
System.out.printf("Main: Steal Count: %d\n", pool.getStealCount()); | |
System.out.printf("******************************************\n"); | |
try { | |
TimeUnit.SECONDS.sleep(10); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} while (!documentTask.isDone()); | |
// Shutdown the pool | |
pool.shutdown(); | |
// Wait for the finalization of the tasks | |
try { | |
pool.awaitTermination(1, TimeUnit.DAYS); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
// Write the results of the tasks | |
try { | |
System.out.printf("Main: The word appears %d in the document", documentTask.get()); | |
} catch (InterruptedException | ExecutionException e) { | |
e.printStackTrace(); | |
} | |
return documentTask.get(); | |
} catch (Exception e) { | |
e.fillInStackTrace(); | |
//System.exit(-1); | |
} | |
return null; | |
}; | |
SearchResult result = forkJoinSearch.searchInFiles(str); | |
result.forEach(System.out::println); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment