Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java
41154 views
1
/*
2
* Copyright (c) 2002, 2019, 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
* @library /test/lib
27
* @bug 4701299
28
* @summary Keep-Alive-Timer thread management in KeepAliveCache causes memory leak
29
* @run main KeepAliveTimerThread
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true KeepAliveTimerThread
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
import jdk.test.lib.net.URIBuilder;
36
import static java.net.Proxy.NO_PROXY;
37
38
public class KeepAliveTimerThread {
39
static class Fetcher implements Runnable {
40
URL url;
41
42
Fetcher(URL url) {
43
this.url = url;
44
}
45
46
public void run() {
47
try {
48
InputStream in = url.openConnection(NO_PROXY).getInputStream();
49
byte b[] = new byte[128];
50
int n;
51
do {
52
n = in.read(b);
53
} while (n > 0);
54
in.close();
55
} catch (Exception x) {
56
x.printStackTrace();
57
}
58
}
59
}
60
61
static class Server extends Thread {
62
ServerSocket server;
63
Server (ServerSocket server) {
64
super ();
65
this.server = server;
66
}
67
void readAll (Socket s) throws IOException {
68
byte[] buf = new byte [128];
69
InputStream is = s.getInputStream ();
70
s.setSoTimeout(1000);
71
try {
72
while (is.read(buf) > 0) ;
73
} catch (SocketTimeoutException x) { }
74
}
75
/*
76
* Our "http" server to return a 404
77
*/
78
public void run() {
79
try {
80
Socket s = server.accept();
81
readAll(s);
82
83
PrintStream out = new PrintStream(
84
new BufferedOutputStream(
85
s.getOutputStream() ));
86
87
/* send the header */
88
out.print("HTTP/1.1 200 OK\r\n");
89
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
90
out.print("Content-Length: 78\r\n");
91
out.print("\r\n");
92
out.print("<HTML>");
93
out.print("<HEAD><TITLE>File Content</TITLE></HEAD>");
94
out.print("<BODY>A dummy body.</BODY>");
95
out.print("</HTML>");
96
out.flush();
97
98
s.close();
99
} catch (Exception e) {
100
e.printStackTrace();
101
} finally {
102
try { server.close(); } catch (IOException unused) {}
103
}
104
}
105
}
106
107
108
public static void main(String args[]) throws Exception {
109
InetAddress loopback = InetAddress.getLoopbackAddress();
110
ServerSocket ss = new ServerSocket();
111
ss.bind(new InetSocketAddress(loopback, 0));
112
Server s = new Server(ss);
113
s.start();
114
115
URL url = URIBuilder.newBuilder()
116
.scheme("http")
117
.loopback()
118
.port(ss.getLocalPort())
119
.toURL();
120
System.out.println("URL: " + url);
121
122
// start fetch in its own thread group
123
ThreadGroup grp = new ThreadGroup("MyGroup");
124
125
// http request in another thread group
126
Thread thr = new Thread(grp, new Fetcher(url));
127
thr.start();
128
thr.join();
129
130
// fetcher is done - the group should now be empty
131
if (grp.activeCount() > 0) {
132
throw new RuntimeException("Keep-alive thread started in wrong thread group");
133
}
134
135
grp.destroy();
136
}
137
138
}
139
140