-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolQueue.java
More file actions
42 lines (38 loc) · 1.23 KB
/
ThreadPoolQueue.java
File metadata and controls
42 lines (38 loc) · 1.23 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
import com.wade.thread.*;
import java.net.*;
import java.io.*;
import java.util.concurrent.*;
// import java.util.concurrent.TimeUnit;
// import java.util.concurrent.ThreadPoolExecutor;
// import java.util.concurrent.ExecutorService;
// import java.util.concurrent.Executors;
public class ThreadPoolQueue {
public static ExecutorService newBoundedFixedThreadPool(int nThreads,int capacity){
return new ThreadPoolExecutor(nThreads,
nThreads,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(capacity),
new ThreadPoolExecutor.DiscardPolicy());
}
public static void boundedThreadPoolServerSocket() throws IOException {
ServerSocket listener = new ServerSocket(8080);
ExecutorService executor = newBoundedFixedThreadPool(4, 16);
try {
while (true) {
Socket socket = listener.accept();
executor.submit(new HandleRequestRunnable(socket));
}
} finally {
listener.close();
}
}
public static void main(String[] args) {
try{
boundedThreadPoolServerSocket();
}
catch(Exception e){
e.printStackTrace();
}
}
}