# HG changeset patch # User bpb # Date 1489535039 25200 # Node ID aeeafa7cccacd98de77124f6a1ad47057bfa74aa # Parent 1c784ab112e1c0552c699a084e315a25bb237493 8176744: Improve internal timing of java/nio/channels/Selector/SelectAndClose.java Summary: Replace two sleeps with CountDownLatch+sleep and Thread.join() Reviewed-by: rriggs, alanb diff -r 1c784ab112e1 -r aeeafa7cccac jdk/test/java/nio/channels/Selector/SelectAndClose.java --- a/jdk/test/java/nio/channels/Selector/SelectAndClose.java Tue Mar 14 15:52:41 2017 -0700 +++ b/jdk/test/java/nio/channels/Selector/SelectAndClose.java Tue Mar 14 16:43:59 2017 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,19 +29,22 @@ import java.nio.channels.*; import java.io.IOException; +import java.util.concurrent.CountDownLatch; public class SelectAndClose { static Selector selector; - static boolean awakened = false; - static boolean closed = false; + static volatile boolean awakened = false; + static volatile boolean closed = false; public static void main(String[] args) throws Exception { selector = Selector.open(); // Create and start a selector in a separate thread. + final CountDownLatch selectLatch = new CountDownLatch(1); new Thread(new Runnable() { public void run() { try { + selectLatch.countDown(); selector.select(); awakened = true; } catch (IOException e) { @@ -51,10 +54,11 @@ }).start(); // Wait for above thread to get to select() before we call close. - Thread.sleep(3000); + selectLatch.await(); + Thread.sleep(2000); // Try to close. This should wakeup select. - new Thread(new Runnable() { + Thread closeThread = new Thread(new Runnable() { public void run() { try { selector.close(); @@ -63,10 +67,11 @@ System.err.println(e); } } - }).start(); + }); + closeThread.start(); // Wait for select() to be awakened, which should be done by close. - Thread.sleep(3000); + closeThread.join(); if (!awakened) selector.wakeup();