< 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,10 +37,12 @@
  * are not closed by the keep-alive timer.
  */
 
 import java.net.*;
 import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
 
 public class MultiThreadTest extends Thread {
 
     /*
      * Is debugging enabled - start with -d to enable.

@@ -166,10 +168,11 @@
      */
     class Server extends Thread {
         ServerSocket ss;
         int connectionCount;
         boolean shutdown = false;
+        List<Worker> workers = new ArrayList<>();
 
         Server(ServerSocket ss) {
             this.ss = ss;
         }
 

@@ -177,10 +180,12 @@
             return connectionCount;
         }
 
         public synchronized void shutdown() {
             shutdown = true;
+            for (Worker worker : workers)
+                worker.stopWorker();
         }
 
         public void run() {
             try {
                 ss.setSoTimeout(2000);

@@ -201,15 +206,17 @@
                         }
                         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) {

@@ -227,16 +234,23 @@
      * multiple http requests on same connection.
      */
     class Worker extends Thread {
         Socket s;
         int id;
+        volatile boolean stopped;
 
         Worker(Socket s, int id) {
             this.s = s;
             this.id = id;
         }
 
+        void stopWorker() {
+            stopped = true;
+            try { s.close(); }
+            catch (IOException ignore) { }
+        }
+
         static int requests = 0;
         static Object rlock = new Object();
 
         public static int getRequests () {
             synchronized (rlock) {

@@ -311,10 +325,11 @@
                         return;
                     }
                 }
 
             } catch (Exception e) {
+                if (!stopped)
                 e.printStackTrace();
             } finally {
                 try {
                     s.close();
                 } catch (Exception e) { }
< prev index next >