Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/AsynchronousChannelGroup/Unbounded.java
41152 views
1
/*
2
* Copyright (c) 2008, 2013, 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
/* @test
25
* @bug 4607272 6842687
26
* @summary Unit test for AsynchronousChannelGroup
27
*/
28
29
import java.nio.ByteBuffer;
30
import java.nio.channels.*;
31
import java.net.*;
32
import java.util.concurrent.*;
33
import java.io.IOException;
34
35
public class Unbounded {
36
// number of concurrent completion handlers
37
static final int CONCURRENCY_COUNT = 256;
38
39
// set to true if an I/O operation fails
40
static volatile boolean failed;
41
42
// set to true when the test is done
43
static volatile boolean finished;
44
45
public static void main(String[] args) throws Exception {
46
// create listener to accept connections
47
AsynchronousServerSocketChannel listener =
48
AsynchronousServerSocketChannel.open()
49
.bind(new InetSocketAddress(0));
50
51
// establish connections
52
53
AsynchronousSocketChannel[] clients = new AsynchronousSocketChannel[CONCURRENCY_COUNT];
54
AsynchronousSocketChannel[] peers = new AsynchronousSocketChannel[CONCURRENCY_COUNT];
55
56
int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
57
SocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), port);
58
59
for (int i=0; i<CONCURRENCY_COUNT; i++) {
60
clients[i] = AsynchronousSocketChannel.open();
61
Future<Void> result = clients[i].connect(sa);
62
peers[i] = listener.accept().get();
63
result.get();
64
}
65
System.out.println("All connection established.");
66
67
// the barrier where all threads (plus the main thread) wait
68
final CyclicBarrier barrier = new CyclicBarrier(CONCURRENCY_COUNT+1);
69
70
// initiate a read operation on each channel.
71
for (AsynchronousSocketChannel client: clients) {
72
ByteBuffer buf = ByteBuffer.allocateDirect(100);
73
client.read(buf, client,
74
new CompletionHandler<Integer,AsynchronousSocketChannel>() {
75
public void completed(Integer bytesRead, AsynchronousSocketChannel ch) {
76
try {
77
ch.close();
78
barrier.await();
79
} catch (Exception x) {
80
throw new AssertionError(x);
81
}
82
}
83
public void failed(Throwable exc, AsynchronousSocketChannel ch) {
84
failed = true;
85
System.err.println("read failed: " + exc);
86
completed(0, ch);
87
}
88
});
89
}
90
System.out.println("All read operations outstanding.");
91
92
// write data to each of the accepted connections
93
for (AsynchronousSocketChannel peer: peers) {
94
peer.write(ByteBuffer.wrap("welcome".getBytes())).get();
95
peer.shutdownOutput();
96
peer.close();
97
}
98
99
// wait for all threads to reach the barrier
100
System.out.println("Waiting for all threads to reach barrier");
101
barrier.await();
102
103
// finish up
104
finished = true;
105
listener.close();
106
if (failed)
107
throw new RuntimeException("I/O operation failed, see log for details");
108
}
109
}
110
111