public class HelloWorld {
public static void main(String[] args) {
System.out.println("This program is running on: " + Thread.currentThread());
System.out.println("Hello world");
}
}
public class MyThread extends Thread {
@Override
public void run() {
System.out.println("Executing code from: " + Thread.currentThread());
System.out.println("Hello world");
}
}
public class Playground {
public static void main(String[] args) {
System.out.println("Creating a new thread from : " + Thread.currentThread());
var myThread = new MyThread();
myThread.start();
System.out.println("Leaving from: " + Thread.currentThread());
}
}
public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Using Runnable from: " + Thread.currentThread());
System.out.println("Hello world");
}
}
public class Playground {
public static void main(String[] args) {
System.out.println("Creating a new thread from : " + Thread.currentThread());
var myRunnable = new MyRunnable();
var thread = new Thread(myRunnable);
thread.start();
System.out.println("Leaving from: " + Thread.currentThread());
}
}
var thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Using Runnable from: " + Thread.currentThread());
System.out.println("Hello world");
}
});
thread.start();
var thread = new Thread(() -> {
System.out.println("Using Runnable from: " + Thread.currentThread());
System.out.println("Hello world");
});
thread.start();
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.util.stream.Collectors;
public class SingleThreadedServer {
private final MostFrequentWordService mostFrequentWordService = new MostFrequentWordService();
public SingleThreadedServer(int port) throws IOException {
var serverSocket = new ServerSocket(port);
while (true) {
var socket = serverSocket.accept();
handle(socket);
}
}
public static void main(String[] args) throws IOException {
new SingleThreadedServer(2222);
}
private void handle(Socket socket) {
System.out.println("Client connected: " + socket);
try (var in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
var out = new PrintWriter(socket.getOutputStream(), true)) {
out.println("Enter url:");
String line;
while ((line = in.readLine()) != null) {
if (isValid(line)) {
var wordCount = mostFrequentWordService.mostFrequentWord(line)
.stream()
.map(counter -> counter.word() + ": " + counter.count())
.collect(Collectors.joining("\n"));
out.println(wordCount);
} else if (line.contains("quit")) {
out.println("Goodbye!");
socket.close();
} else {
out.println("Malformed URL");
out.println("Enter url:");
}
}
} catch (IOException e) {
System.out.println("Was unable to establish or communicate with client socket:" + e.getMessage());
}
}
private static boolean isValid(String url) {
try {
new URL(url);
} catch (MalformedURLException e) {
System.out.println("invalid url: " + url);
return false;
}
return true;
}
}
import org.jsoup.Jsoup;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
record WordCount(String word, long count) {
}
public class MostFrequentWordService {
public List<WordCount> mostFrequentWord(String url) throws IOException {
var wordCount = Arrays.stream(getWords(url))
.filter(value -> !value.isEmpty())
.filter(value -> value.length() > 3)
.map(String::toLowerCase)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
return wordCount.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(5)
.map(entry -> new WordCount(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
}
private String[] getWords(String url) throws IOException {
var connect = Jsoup.connect(url);
var document = connect.get();
var content = document.body().text();
return content.split("[^a-zA-Z]");
}
}
public class MultiThreadedServer {
private final MostFrequentWordService mostFrequentWordService = new MostFrequentWordService();
public MultiThreadedServer(int port) throws IOException {
var serverSocket = new ServerSocket(port);
while (true) {
var socket = serverSocket.accept();
var thread = new Thread(() -> handle(socket));
thread.start();
}
}
// оставшийся код будет без изменений
}