-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadpool.java
More file actions
59 lines (42 loc) · 1.93 KB
/
Threadpool.java
File metadata and controls
59 lines (42 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Logger;
import static util.LogSystemStats.logSystemStats;
public class Threadpool {
public static void main(String[] args) throws IOException {
System.out.println("HTTP Server is running...");
HttpServer server = HttpServer.create(new InetSocketAddress(8084), 0);
server.createContext("/send", new VirtualThreadHttpServer.SendMessageHandler());
server.createContext("/messages", new VirtualThreadHttpServer.GetMessageHandler());
server.createContext("/login", new LoginHandler());
server.createContext("/logout", new LogoutHandler());
server.createContext("/register", new RegisterHandler());
server.createContext("/ping", new PingPongHandler());
// 🔁 Use traditional thread pool (pre-Java 21)
int poolSize = Runtime.getRuntime().availableProcessors() * 2;
System.out.println("Using thread pool size: " + poolSize);
ExecutorService executor = Executors.newFixedThreadPool(poolSize);
server.setExecutor(executor);
server.start();
System.out.println("Server started on port 8082 with thread pool size: " + poolSize);
ScheduledExecutorService statsLogger = Executors.newSingleThreadScheduledExecutor();
new Thread(() -> {
while (true) {
logSystemStats();
try {
Thread.sleep(5000); // 5 seconds in milliseconds
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}).start();
// every 5 seconds
}
}