Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java
38867 views
1
/*
2
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 4701299
27
* @summary Keep-Alive-Timer thread management in KeepAliveCache causes memory leak
28
*/
29
import java.net.*;
30
import java.io.*;
31
32
public class KeepAliveTimerThread {
33
static class Fetcher implements Runnable {
34
String url;
35
36
Fetcher(String url) {
37
this.url = url;
38
}
39
40
public void run() {
41
try {
42
InputStream in =
43
(new URL(url)).openConnection().getInputStream();
44
byte b[] = new byte[128];
45
int n;
46
do {
47
n = in.read(b);
48
} while (n > 0);
49
in.close();
50
} catch (Exception x) {
51
x.printStackTrace();
52
}
53
}
54
}
55
56
static class Server extends Thread {
57
ServerSocket server;
58
Server (ServerSocket server) {
59
super ();
60
this.server = server;
61
}
62
void readAll (Socket s) throws IOException {
63
byte[] buf = new byte [128];
64
InputStream is = s.getInputStream ();
65
s.setSoTimeout(1000);
66
try {
67
while (is.read(buf) > 0) ;
68
} catch (SocketTimeoutException x) { }
69
}
70
/*
71
* Our "http" server to return a 404
72
*/
73
public void run() {
74
try {
75
Socket s = server.accept();
76
readAll(s);
77
78
PrintStream out = new PrintStream(
79
new BufferedOutputStream(
80
s.getOutputStream() ));
81
82
/* send the header */
83
out.print("HTTP/1.1 200 OK\r\n");
84
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
85
out.print("Content-Length: 78\r\n");
86
out.print("\r\n");
87
out.print("<HTML>");
88
out.print("<HEAD><TITLE>File Content</TITLE></HEAD>");
89
out.print("<BODY>A dummy body.</BODY>");
90
out.print("</HTML>");
91
out.flush();
92
93
s.close();
94
} catch (Exception e) {
95
e.printStackTrace();
96
} finally {
97
try { server.close(); } catch (IOException unused) {}
98
}
99
}
100
}
101
102
103
public static void main(String args[]) throws Exception {
104
ServerSocket ss = new ServerSocket(0);
105
Server s = new Server (ss);
106
s.start();
107
108
String url = "http://127.0.0.1:"+ss.getLocalPort();
109
110
// start fetch in its own thread group
111
ThreadGroup grp = new ThreadGroup("MyGroup");
112
113
// http request in another thread group
114
Thread thr = new Thread(grp, new Fetcher(url));
115
thr.start();
116
thr.join();
117
118
// fetcher is done - the group should now be empty
119
if (grp.activeCount() > 0) {
120
throw new RuntimeException("Keep-alive thread started in wrong thread group");
121
}
122
123
grp.destroy();
124
}
125
126
}
127
128