< prev index next >

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

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2002, 2010, 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. --- 1,7 ---- /* ! * 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,53 **** * are not closed by the keep-alive timer. */ import java.net.*; import java.io.*; public class MultiThreadTest extends Thread { /* * Is debugging enabled - start with -d to enable. */ ! static boolean debug = false; static Object threadlock = new Object (); static int threadCounter = 0; static Object getLock() { return threadlock; } --- 37,56 ---- * are not closed by the keep-alive timer. */ import java.net.*; import java.io.*; + import java.time.Duration; + import java.util.Queue; + import java.util.concurrent.ConcurrentLinkedQueue; public class MultiThreadTest extends Thread { /* * Is debugging enabled - start with -d to enable. */ ! static boolean debug = true; // disable debug once stability proven static Object threadlock = new Object (); static int threadCounter = 0; static Object getLock() { return threadlock; }
*** 92,101 **** --- 95,106 ---- threadCounter ++; } } public void run () { + long start = System.nanoTime(); + try { for (int i=0; i<requests; i++) { doRequest (uri); } } catch (Exception e) {
*** 106,120 **** --- 111,127 ---- 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,164 **** --- 162,176 ---- 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,180 **** --- 178,197 ---- */ class Server extends Thread { ServerSocket ss; int connectionCount; boolean shutdown = false; + private Queue<Worker> workers = new ConcurrentLinkedQueue<>(); Server(ServerSocket ss) { this.ss = ss; } + public Queue<Worker> workers() { + return workers; + } + public synchronized int connectionCount() { return connectionCount; } public synchronized void shutdown() {
*** 201,215 **** } continue; } int id; synchronized (this) { id = connectionCount++; } - - Worker w = new Worker(s, id); w.start(); MultiThreadTest.debug("server: Started worker " + id); } } catch (Exception e) { --- 218,233 ---- } continue; } int id; + Worker w; synchronized (this) { id = connectionCount++; + w = new Worker(s, id); + workers.add(w); } w.start(); MultiThreadTest.debug("server: Started worker " + id); } } catch (Exception e) {
*** 266,275 **** --- 284,295 ---- } } } 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,323 **** --- 336,345 ---- e.printStackTrace(); } finally { try { s.close(); } catch (Exception e) { } + MultiThreadTest.debug("worker: " + id + " end - " + + Duration.ofNanos(System.nanoTime() - start)); } } }
< prev index next >