test/jdk/java/nio/channels/Selector/SelectAndClose.java
changeset 51602 dbb0e798deeb
parent 50324 a29b954314d2
--- a/test/jdk/java/nio/channels/Selector/SelectAndClose.java	Thu Aug 30 17:59:40 2018 -0700
+++ b/test/jdk/java/nio/channels/Selector/SelectAndClose.java	Fri Aug 31 10:00:22 2018 +0800
@@ -23,33 +23,18 @@
 
 /* @test
  * @bug 5004077 8203765
+ * @build SelectorUtils
+ * @run main SelectAndClose
  * @summary Check blocking of select and close
  */
 
 import java.io.IOException;
-import java.lang.management.ManagementFactory;
-import java.lang.management.MonitorInfo;
-import java.lang.management.ThreadInfo;
 import java.nio.channels.Selector;
 
 public class SelectAndClose {
     static Selector selector;
     static volatile boolean awakened = false;
 
-    private static boolean mightHoldLock(Thread t, Object lock) {
-        long tid = t.getId();
-        int hash = System.identityHashCode(lock);
-        ThreadInfo ti = ManagementFactory.getThreadMXBean().
-            getThreadInfo(new long[]{ tid} , true, false, 100)[0];
-        if (ti != null) {
-            for (MonitorInfo mi : ti.getLockedMonitors()) {
-                if (mi.getIdentityHashCode() == hash)
-                    return true;
-            }
-        }
-        return false;
-    }
-
     public static void main(String[] args) throws Exception {
         selector = Selector.open();
 
@@ -66,12 +51,8 @@
             });
         selectThread.start();
 
-        // Spin until the monitor of the selected-key set is likely held
-        // as selected operations are specified to synchronize on the
-        // selected-key set.
-        while (!mightHoldLock(selectThread, selector.selectedKeys())) {
-            Thread.sleep(50);
-        }
+        // spin until make sure select is invoked
+        SelectorUtils.spinUntilLocked(selectThread, selector);
 
         // Close the selector.
         selector.close();