Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

многопоточность дз #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions parallelism/src/main/java/ru/hh/school/homework/FolderThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ru.hh.school.homework;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

public class FolderThread extends Thread{

private List<File> directory;
List<File> folders = new ArrayList<>();
private int countFolder;
private Map<Path, List<Path>> folderToFilePaths = new HashMap<>();
private final static Random random = new Random();

public FolderThread(List<File> directory) {
this.directory = directory;
this.countFolder = 15;
}

@Override
public void run() {
scanDirectoryRecursive(directory);
if (folders.size() > 0) {
new FolderThread(folders).start();
}
folderToFilePaths.entrySet().forEach(item -> {
new ScanFileThread(item.getKey(), item.getValue()).start();
});
}
public void scanDirectoryRecursive(List<File> directories) {
for (File directory: directories) {
if (Objects.nonNull(directory.list())) {
for (File file: directory.listFiles()) {
if (file.isFile() && file.getName().endsWith(".java")) {
Path path = file.toPath();
Path folder = path.subpath(0, path.getNameCount() - 1);
if (!folderToFilePaths.containsKey(folder)) {
List<Path> paths = new ArrayList<>();
paths.add(path);
folderToFilePaths.put(folder, paths);
} else {
folderToFilePaths.get(folder).add(path);
}
} else if (file.isDirectory()) {
folders.add(file);
if(folders.size() == countFolder) {
new FolderThread(new ArrayList<>(folders)).start();
folders = new ArrayList<>();
}
}
}
}
}
}
}
49 changes: 49 additions & 0 deletions parallelism/src/main/java/ru/hh/school/homework/GoogleThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ru.hh.school.homework;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class GoogleThread extends Thread {

private Path folder;
private List<String> worlds;

public GoogleThread(Path folder, List<String> worlds) {
this.folder = folder;
this.worlds = worlds;
}
@Override
public void run() {
worlds.forEach(this::naiveSearch);
}

private void naiveSearch(String query) {
Document document;
try {
document = Jsoup
.connect("https://www.google.com/search?q=" + query)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
.get();
} catch (IOException e) {
System.out.println(getDescriptionResult(query, "GOOGLE BAN"));
return;
}
Element divResultStats = document.select("div#slim_appbar").first();
String text = divResultStats.text();
if ("".equals(text)) {
System.out.println(getDescriptionResult(query, "NO GOOGLE RESULT"));
return;
}
String result = text.substring(0, text.indexOf('(')).replaceAll("\\D", "");
System.out.println(getDescriptionResult(query, result));
}

private String getDescriptionResult(String word, String googleResult) {
return String.format("Folder: %s;\n Word: %s; Approximate number of results from Google: %s", folder, word, googleResult);
}

}
13 changes: 13 additions & 0 deletions parallelism/src/main/java/ru/hh/school/homework/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.hh.school.homework;

import java.io.File;
import java.util.List;

public class Main {
public static void main(String[] args) throws InterruptedException {
//Path path = Path.of("d:\\projects\\work\\hh-school\\parallelism\\src\\main\\java\\ru\\hh\\school\\parallelism\\Runner.java");
String string = "C:\\Users\\nedomets.aleksandr\\IdeaProjects";
File file = new File(string);
if (file.isDirectory()) new FolderThread(List.of(file)).start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ru.hh.school.homework;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.Collections.reverseOrder;
import static java.util.Map.Entry.comparingByValue;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toMap;

public class ScanFileThread extends Thread {

private List<Path> files;
private Path folder;

public ScanFileThread(Path folder, List<Path> files) {
this.folder = folder;
this.files = files;
}

@Override
public void run() {
List<String> top10Words = top10Words(files);
new GoogleThread(folder, top10Words.subList(0, 5)).start();
new GoogleThread(folder, top10Words.subList(5, 10)).start();
}

private static Map<String, Long> naiveCount(Path path) {
try {
return Files.lines(path)
.flatMap(line -> Stream.of(line.split("[^a-zA-Z\\d]")))
.filter(word -> word.length() > 3)
.collect(groupingBy(identity(), counting()))
.entrySet()
.stream()
.sorted(comparingByValue(reverseOrder()))
.limit(10)
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static List<String> top10Words(List<Path> files) {
return files.stream()
.flatMap(file -> naiveCount(file).entrySet().stream())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, Long::sum))
.entrySet()
.stream()
.sorted(comparingByValue(reverseOrder()))
.limit(10)
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue))
.keySet().stream().toList();
}

}