From b0b99bdc3fa58b37b813e465cabbeb067dc19ffa Mon Sep 17 00:00:00 2001 From: Stefan Ring Date: Wed, 8 Oct 2008 10:32:25 +0200 Subject: [PATCH 1/1] * tests/threads/threadpooltest.java: Added test program. --- tests/threads/threadpooltest.java | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/threads/threadpooltest.java diff --git a/tests/threads/threadpooltest.java b/tests/threads/threadpooltest.java new file mode 100644 index 000000000..3094c96ff --- /dev/null +++ b/tests/threads/threadpooltest.java @@ -0,0 +1,51 @@ +// Use with "nc localhost 8080 < /dev/null" +// Idle threads are parked by the jsr166 framework and caused lots of CPU usage +// before the addition of park/unpark to CACAO. + +import java.net.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.net.InetSocketAddress; + +public class threadpooltest { + private static final int NTHREADS = 3; + private static final Executor exec = Executors.newFixedThreadPool(NTHREADS); + + private static void handleRequest(Socket s) { + String ss = "hello"; + try { + if (Thread.currentThread().isInterrupted()) + ss += " (wasinterrupted)"; + ss += " " + Thread.currentThread().toString(); + PrintWriter pw = new PrintWriter(s.getOutputStream()); + //Thread.currentThread().interrupt(); + pw.println(ss); + pw.flush(); + Thread.sleep(100); + pw.println("closing"); + pw.flush(); + s.close(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) throws IOException { + ServerSocket socket = new ServerSocket(); + socket.setReuseAddress(true); + socket.bind(new InetSocketAddress(8080)); + while (true) { + final Socket connection = socket.accept(); + Runnable task = new Runnable() { + public void run() { + handleRequest(connection); + } + }; + exec.execute(task); + } + } +} -- 2.25.1