// security manager grants all permissions var securityManager = new SecurityManager() {
@Override publicvoid checkPermission(Permission perm) { }
};
System.setSecurityManager(securityManager);
Deque<Socket> sockets = new ArrayDeque<>(); try { // create the maximum number of sockets for (int i=0; i<limit; i++) {
sockets.offer(newUdpSocket());
}
// try to create another socket - should fail try {
Socket s = newUdpSocket();
s.close();
assertTrue(false);
} catch (IOException expected) { }
// close one socket
sockets.pop().close();
// try to create another socket - should succeed
Socket s = newUdpSocket();
// unreference the socket and wait for it to be closed by the cleaner var ref = new WeakReference<>(s);
s = null; while (ref.get() != null) {
System.gc(); Thread.sleep(100);
}
// try to create another socket - should succeed
s = newUdpSocket();
s.close();
} finally {
closeAll(sockets);
System.setSecurityManager(null);
}
}
private Socket newUdpSocket() throws IOException {
Socket s = null;
try {
s = new Socket(InetAddress.getLoopbackAddress(), 8000, false);
} catch (BindException unexpected) {
System.out.println("BindException caught retry Socket creation");
s = new Socket(InetAddress.getLoopbackAddress(), 8000, false);
} return s;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.