8176744: Improve internal timing of java/nio/channels/Selector/SelectAndClose.java

Replace two sleeps with CountDownLatch+sleep and Thread.join()

Reviewed-by: rriggs, alanb
This commit is contained in:
Brian Burkhalter 2017-03-14 16:43:59 -07:00
parent 2a6774809a
commit 990e5a380e

View File

@ -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 @@ public class SelectAndClose {
}).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 @@ public class SelectAndClose {
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();