class HelloWorld {
public static void main(String[] args) {
System.out.println("This program is running on: " + Thread.currentThread());
System.out.println("Hello world");
}
}
class MyThread extends Thread {
@Override
public void run() {
System.out.println("Executing code from: " + Thread.currentThread());
System.out.println("Hello world");
}
}
class HelloWorld {
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());
}
}
class MyThread extends Thread {
@Override
public void run() {
System.out.println("run() выполняется в потоке: " + Thread.currentThread().getName());
}
}
class HelloWorld {
public static void main(String[] args) {
MyThread thread = new MyThread();
System.out.println("Вызываем run() напрямую:");
thread.run(); // Выполнится в main-потоке
System.out.println("Вызываем start():");
thread.start(); // Создаст новый поток
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Using Runnable from: " + Thread.currentThread());
System.out.println("Hello world");
}
}
class HelloWorld {
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.ServerSocket;
import java.net.Socket;
import java.util.stream.Collectors;
import java.net.URI;
public class SingleThreadedServer {
private final MostFrequentWordService wordService = new MostFrequentWordService();
public SingleThreadedServer(int port) throws IOException {
try (var serverSocket = new ServerSocket(port)) {
while (true) {
try (var socket = serverSocket.accept()) {
handle(socket);
}
}
}
}
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 = wordService.mostFrequentWord(line).stream()
.map(counter -> counter.word() + ": " + counter.count())
.collect(Collectors.joining(System.lineSeparator()));
out.println(wordCount);
} else if (line.contains("quit")) {
out.println("Goodbye!");
break;
} else {
out.println("Malformed URL");
}
out.println("\nEnter url:");
}
} catch (IOException e) {
System.out.println("Communication error: " + e.getMessage());
}
}
private static boolean isValid(String url) {
try {
URI.create(url).toURL();
return true;
} catch (Exception e) {
System.out.println("invalid url: " + url);
return false;
}
}
public static void main(String[] args) throws IOException {
new SingleThreadedServer(2222);
}
}
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 {
List<WordCount> mostFrequentWord(String url) throws IOException {
return Arrays.stream(getWords(url))
.filter(word -> word.length() > 3)
.map(String::toLowerCase)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(5)
.map(entry -> new WordCount(entry.getKey(), entry.getValue()))
.toList();
}
private String[] getWords(String url) throws IOException {
return Jsoup.connect(url).get().body().text().split("\\W+");
}
}
public class MultiThreadedServer {
private final MostFrequentWordService wordService = new MostFrequentWordService();
public MultiThreadedServer(int port) throws IOException {
try (var serverSocket = new ServerSocket(port)) {
while (true) {
var socket = serverSocket.accept();
var thread = new Thread(() -> handle(socket));
thread.start();
}
}
}
// оставшийся код будет без изменений
}