--- a/src/java.base/share/classes/java/net/Socket.java Tue Apr 09 12:02:23 2019 +0100
+++ b/src/java.base/share/classes/java/net/Socket.java Sat Apr 13 07:22:55 2019 +0100
@@ -35,6 +35,7 @@
import java.security.PrivilegedAction;
import java.util.Set;
import java.util.Collections;
+import java.util.concurrent.locks.ReentrantLock;
/**
* This class implements client sockets (also called just
@@ -62,7 +63,7 @@
private boolean bound = false;
private boolean connected = false;
private boolean closed = false;
- private Object closeLock = new Object();
+ private final ReentrantLock closeLock = new ReentrantLock();
private boolean shutIn = false;
private boolean shutOut = false;
@@ -1573,13 +1574,20 @@
* @spec JSR-51
* @see #isClosed
*/
- public synchronized void close() throws IOException {
- synchronized(closeLock) {
- if (isClosed())
- return;
- if (created)
- impl.close();
- closed = true;
+ public void close() throws IOException {
+ closeLock.lock();
+ try {
+ if (!closed) {
+ try {
+ SocketImpl impl = this.impl;
+ if (impl != null)
+ impl.close();
+ } finally {
+ closed = true;
+ }
+ }
+ } finally {
+ closeLock.unlock();
}
}
@@ -1700,8 +1708,11 @@
* @see #close
*/
public boolean isClosed() {
- synchronized(closeLock) {
+ closeLock.lock();
+ try {
return closed;
+ } finally {
+ closeLock.unlock();
}
}