PR123: LD_LIBRARY_PATH and java.library.path
[cacao.git] / tests / threads / threadpooltest.java
1 // Use with "nc localhost 8080 < /dev/null"
2 // Idle threads are parked by the jsr166 framework and caused lots of CPU usage
3 // before the addition of park/unpark to CACAO.
4
5 import java.net.*;
6 import java.io.IOException;
7 import java.io.PrintWriter;
8 import java.util.concurrent.Executor;
9 import java.util.concurrent.Executors;
10 import java.net.InetSocketAddress;
11
12 public class threadpooltest {
13         private static final int NTHREADS = 3;
14         private static final Executor exec = Executors.newFixedThreadPool(NTHREADS);
15         
16         private static void handleRequest(Socket s) {
17                 String ss = "hello";
18                 try {
19                         if (Thread.currentThread().isInterrupted())
20                                 ss += " (wasinterrupted)";
21                         ss += " " + Thread.currentThread().toString();
22                         PrintWriter pw = new PrintWriter(s.getOutputStream());
23                         //Thread.currentThread().interrupt();
24                         pw.println(ss);
25                         pw.flush();
26                         Thread.sleep(100);
27                         pw.println("closing");
28                         pw.flush();
29                         s.close();
30                 } catch (InterruptedException e) {
31                         e.printStackTrace();
32                 } catch (IOException e) {
33                         e.printStackTrace();
34                 }
35         }
36
37         public static void main(String[] args) throws IOException {
38                 ServerSocket socket = new ServerSocket();
39                 socket.setReuseAddress(true);
40                 socket.bind(new InetSocketAddress(8080));
41                 while (true) {
42                         final Socket connection = socket.accept();
43                         Runnable task = new Runnable() {
44                                 public void run() {
45                                         handleRequest(connection);
46                                 }
47                         };
48                         exec.execute(task);
49                 }
50         }
51 }