test/jdk/java/nio/channels/Selector/SelectAndClose.java
changeset 51602 dbb0e798deeb
parent 50324 a29b954314d2
equal deleted inserted replaced
51601:e469480420dc 51602:dbb0e798deeb
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 5004077 8203765
    25  * @bug 5004077 8203765
       
    26  * @build SelectorUtils
       
    27  * @run main SelectAndClose
    26  * @summary Check blocking of select and close
    28  * @summary Check blocking of select and close
    27  */
    29  */
    28 
    30 
    29 import java.io.IOException;
    31 import java.io.IOException;
    30 import java.lang.management.ManagementFactory;
       
    31 import java.lang.management.MonitorInfo;
       
    32 import java.lang.management.ThreadInfo;
       
    33 import java.nio.channels.Selector;
    32 import java.nio.channels.Selector;
    34 
    33 
    35 public class SelectAndClose {
    34 public class SelectAndClose {
    36     static Selector selector;
    35     static Selector selector;
    37     static volatile boolean awakened = false;
    36     static volatile boolean awakened = false;
    38 
       
    39     private static boolean mightHoldLock(Thread t, Object lock) {
       
    40         long tid = t.getId();
       
    41         int hash = System.identityHashCode(lock);
       
    42         ThreadInfo ti = ManagementFactory.getThreadMXBean().
       
    43             getThreadInfo(new long[]{ tid} , true, false, 100)[0];
       
    44         if (ti != null) {
       
    45             for (MonitorInfo mi : ti.getLockedMonitors()) {
       
    46                 if (mi.getIdentityHashCode() == hash)
       
    47                     return true;
       
    48             }
       
    49         }
       
    50         return false;
       
    51     }
       
    52 
    37 
    53     public static void main(String[] args) throws Exception {
    38     public static void main(String[] args) throws Exception {
    54         selector = Selector.open();
    39         selector = Selector.open();
    55 
    40 
    56         // Create and start a selector in a separate thread.
    41         // Create and start a selector in a separate thread.
    64                     }
    49                     }
    65                 }
    50                 }
    66             });
    51             });
    67         selectThread.start();
    52         selectThread.start();
    68 
    53 
    69         // Spin until the monitor of the selected-key set is likely held
    54         // spin until make sure select is invoked
    70         // as selected operations are specified to synchronize on the
    55         SelectorUtils.spinUntilLocked(selectThread, selector);
    71         // selected-key set.
       
    72         while (!mightHoldLock(selectThread, selector.selectedKeys())) {
       
    73             Thread.sleep(50);
       
    74         }
       
    75 
    56 
    76         // Close the selector.
    57         // Close the selector.
    77         selector.close();
    58         selector.close();
    78 
    59 
    79         if (!awakened)
    60         if (!awakened)