< prev index next >

test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -37,17 +37,20 @@
  * are not closed by the keep-alive timer.
  */
 
 import java.net.*;
 import java.io.*;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
 
 public class MultiThreadTest extends Thread {
 
     /*
      * Is debugging enabled - start with -d to enable.
      */
-    static boolean debug = false;
+    static boolean debug = true; // disable debug once stability proven
 
     static Object threadlock = new Object ();
     static int threadCounter = 0;
 
     static Object getLock() { return threadlock; }

@@ -92,10 +95,12 @@
             threadCounter ++;
         }
     }
 
     public void run () {
+        long start = System.nanoTime();
+
         try {
             for (int i=0; i<requests; i++) {
                 doRequest (uri);
             }
         } catch (Exception e) {

@@ -106,15 +111,17 @@
                 if (threadCounter == 0) {
                     threadlock.notifyAll();
                 }
             }
         }
+        debug("client: end - " + Duration.ofNanos(System.nanoTime() - start));
     }
 
     static int threads=5;
 
     public static void main(String args[]) throws Exception {
+        long start = System.nanoTime();
 
         int x = 0, arg_len = args.length;
         int requests = 20;
 
         if (arg_len > 0 && args[0].equals("-d")) {

@@ -155,10 +162,15 @@
             throw new RuntimeException ("Expected "+threads + " connections: used " +cnt);
         }
         if  (reqs != threads*requests) {
             throw new RuntimeException ("Expected "+ threads*requests+ " requests: got " +reqs);
         }
+        for (Thread worker : svr.workers()) {
+            worker.join(60_000);
+        }
+
+        debug("main thread end - " + Duration.ofNanos(System.nanoTime() - start));
     }
 }
 
     /*
      * Server thread to accept connection and create worker threads

@@ -166,15 +178,20 @@
      */
     class Server extends Thread {
         ServerSocket ss;
         int connectionCount;
         boolean shutdown = false;
+        private List<Worker> workers = new ArrayList<>();
 
         Server(ServerSocket ss) {
             this.ss = ss;
         }
 
+        public synchronized List<Worker> workers() {
+            return workers;
+        }
+
         public synchronized int connectionCount() {
             return connectionCount;
         }
 
         public synchronized void shutdown() {

@@ -201,15 +218,16 @@
                         }
                         continue;
                     }
 
                     int id;
+                    Worker w;
                     synchronized (this) {
                         id = connectionCount++;
+                        w = new Worker(s, id);
+                        workers.add(w);
                     }
-
-                    Worker w = new Worker(s, id);
                     w.start();
                     MultiThreadTest.debug("server: Started worker " + id);
                 }
 
             } catch (Exception e) {

@@ -266,10 +284,12 @@
                 }
             }
         }
 
         public void run() {
+            long start = System.nanoTime();
+
             try {
                 int max = 400;
                 byte b[] = new byte[1000];
                 InputStream in = new BufferedInputStream (s.getInputStream());
                 // response to client

@@ -316,8 +336,10 @@
                 e.printStackTrace();
             } finally {
                 try {
                     s.close();
                 } catch (Exception e) { }
+                MultiThreadTest.debug("worker: " + id  + " end - " +
+                            Duration.ofNanos(System.nanoTime() - start));
             }
         }
     }
< prev index next >