Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java
38867 views
/*1* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 470129926* @summary Keep-Alive-Timer thread management in KeepAliveCache causes memory leak27*/28import java.net.*;29import java.io.*;3031public class KeepAliveTimerThread {32static class Fetcher implements Runnable {33String url;3435Fetcher(String url) {36this.url = url;37}3839public void run() {40try {41InputStream in =42(new URL(url)).openConnection().getInputStream();43byte b[] = new byte[128];44int n;45do {46n = in.read(b);47} while (n > 0);48in.close();49} catch (Exception x) {50x.printStackTrace();51}52}53}5455static class Server extends Thread {56ServerSocket server;57Server (ServerSocket server) {58super ();59this.server = server;60}61void readAll (Socket s) throws IOException {62byte[] buf = new byte [128];63InputStream is = s.getInputStream ();64s.setSoTimeout(1000);65try {66while (is.read(buf) > 0) ;67} catch (SocketTimeoutException x) { }68}69/*70* Our "http" server to return a 40471*/72public void run() {73try {74Socket s = server.accept();75readAll(s);7677PrintStream out = new PrintStream(78new BufferedOutputStream(79s.getOutputStream() ));8081/* send the header */82out.print("HTTP/1.1 200 OK\r\n");83out.print("Content-Type: text/html; charset=iso-8859-1\r\n");84out.print("Content-Length: 78\r\n");85out.print("\r\n");86out.print("<HTML>");87out.print("<HEAD><TITLE>File Content</TITLE></HEAD>");88out.print("<BODY>A dummy body.</BODY>");89out.print("</HTML>");90out.flush();9192s.close();93} catch (Exception e) {94e.printStackTrace();95} finally {96try { server.close(); } catch (IOException unused) {}97}98}99}100101102public static void main(String args[]) throws Exception {103ServerSocket ss = new ServerSocket(0);104Server s = new Server (ss);105s.start();106107String url = "http://127.0.0.1:"+ss.getLocalPort();108109// start fetch in its own thread group110ThreadGroup grp = new ThreadGroup("MyGroup");111112// http request in another thread group113Thread thr = new Thread(grp, new Fetcher(url));114thr.start();115thr.join();116117// fetcher is done - the group should now be empty118if (grp.activeCount() > 0) {119throw new RuntimeException("Keep-alive thread started in wrong thread group");120}121122grp.destroy();123}124125}126127128