Tidy OldSocketTest.test_connectLjava_net_SocketAddressI_setSOTimeout.

Improve thread safety and exception handling.

I don't expect this will fix the test flakiness but it eliminates
these as possibilities.

Bug: 144258500
Test: atest --iterations 10 CtsLibcoreTestCases:libcore.java.net.OldSocketTest
Change-Id: Idfa01771afa562482fcfd597abac325f95fe183a
(cherry picked from commit 282ba2d764a259731816682dedf080edbe28db18)
diff --git a/luni/src/test/java/libcore/java/net/OldSocketTest.java b/luni/src/test/java/libcore/java/net/OldSocketTest.java
index 8a577fb..f20f93d 100644
--- a/luni/src/test/java/libcore/java/net/OldSocketTest.java
+++ b/luni/src/test/java/libcore/java/net/OldSocketTest.java
@@ -20,6 +20,8 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.net.BindException;
 import java.net.ConnectException;
 import java.net.Inet4Address;
@@ -37,6 +39,7 @@
 import java.nio.channels.IllegalBlockingModeException;
 import java.nio.channels.SocketChannel;
 import java.security.Permission;
+import java.util.concurrent.atomic.AtomicReference;
 import libcore.junit.util.ResourceLeakageDetector.DisableResourceLeakageDetection;
 import tests.support.Support_Configuration;
 
@@ -1270,35 +1273,33 @@
             why = "Strange threading behavior causes resource leak",
             bug = "31820278")
     public void test_connectLjava_net_SocketAddressI_setSOTimeout() throws Exception {
+        final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
+
         class SocketConnector extends Thread {
+            private final int timeout;
+            private final Socket theSocket;
+            private final SocketAddress address;
 
-            int timeout = 0;
-
-            Socket theSocket = null;
-
-            SocketAddress address = null;
-
+            @Override
             public void run() {
                 try {
                     theSocket.connect(address, timeout);
                 } catch (Exception e) {
+                    exceptionRef.set(e);
                 }
-
-                return;
             }
 
-            public SocketConnector(int timeout, Socket theSocket,
-                    SocketAddress address) {
+            private SocketConnector(int timeout, Socket theSocket, SocketAddress address) {
                 this.timeout = timeout;
                 this.theSocket = theSocket;
                 this.address = address;
             }
         }
 
-        // now try to set options while we are connecting
-        SocketAddress nonReachableAddress = UNREACHABLE_ADDRESS;
-        try (Socket theSocket = new Socket()) {
-            SocketConnector connector = new SocketConnector(5000, theSocket, nonReachableAddress);
+        // Now try to set options while we are connecting
+        try (final Socket theSocket = new Socket()) {
+            final SocketConnector connector
+                = new SocketConnector(5000, theSocket, UNREACHABLE_ADDRESS);
             connector.start();
             theSocket.setSoTimeout(1000);
             Thread.sleep(10);
@@ -1308,10 +1309,21 @@
             theSocket.setSoTimeout(2000);
             assertTrue("Socket option not set during connect: 50 ",
                     Math.abs(2000 - theSocket.getSoTimeout()) <= 10);
-            Thread.sleep(5000);
+            connector.join();
+            Exception e = exceptionRef.get();
+            if (!(e instanceof SocketTimeoutException)) {
+                fail(printStackTraceToString(e));
+            }
         }
     }
 
+    private String printStackTraceToString(Throwable throwable) {
+        StringWriter writer = new StringWriter();
+        throwable.printStackTrace(new PrintWriter(writer));
+        return writer.toString();
+    }
+
+
     public void test_isInputShutdown() throws IOException {
         Socket theSocket = new Socket();
         ServerSocket serverSocket = new ServerSocket(0, 5);