jdk/test/java/nio/channels/DatagramChannel/EmptyBuffer.java
changeset 5788 50d4ad1a780f
parent 5506 202f599c92aa
child 5970 d4e98bbfb0be
--- a/jdk/test/java/nio/channels/DatagramChannel/EmptyBuffer.java	Sun Jun 13 17:19:22 2010 -0700
+++ b/jdk/test/java/nio/channels/DatagramChannel/EmptyBuffer.java	Tue Jun 15 10:03:37 2010 +0100
@@ -42,18 +42,16 @@
     }
 
     static void test() throws Exception {
-        Sprintable server = new Server();
+        Server server = new Server();
         Thread serverThread = new Thread(server);
         serverThread.start();
-        while (!server.ready())
-            Thread.sleep(50);
         DatagramChannel dc = DatagramChannel.open();
         ByteBuffer bb = ByteBuffer.allocateDirect(12);
         bb.order(ByteOrder.BIG_ENDIAN);
         bb.putInt(1).putLong(1);
         bb.flip();
         InetAddress address = InetAddress.getLocalHost();
-        InetSocketAddress isa = new InetSocketAddress(address, 8888);
+        InetSocketAddress isa = new InetSocketAddress(address, server.port());
         dc.connect(isa);
         dc.write(bb);
         bb.rewind();
@@ -65,24 +63,23 @@
         server.throwException();
     }
 
-    public interface Sprintable extends Runnable {
-        public void throwException() throws Exception;
-        public boolean ready();
-    }
+    public static class Server implements Runnable {
+        final DatagramChannel dc;
+        Exception e = null;
 
-    public static class Server implements Sprintable {
-        Exception e = null;
-        private volatile boolean ready = false;
+        Server() throws IOException {
+            this.dc = DatagramChannel.open().bind(new InetSocketAddress(0));
+        }
 
-        public void throwException() throws Exception {
+        int port() {
+            return dc.socket().getLocalPort();
+        }
+
+        void throwException() throws Exception {
             if (e != null)
                 throw e;
         }
 
-        public boolean ready() {
-            return ready;
-        }
-
         void showBuffer(String s, ByteBuffer bb) {
             log.println(s);
             bb.rewind();
@@ -97,9 +94,6 @@
             SocketAddress sa = null;
             int numberReceived = 0;
             try {
-                DatagramChannel dc = DatagramChannel.open();
-                dc.socket().bind(new InetSocketAddress(8888));
-                ready = true;
                 ByteBuffer bb = ByteBuffer.allocateDirect(12);
                 bb.clear();
                 // Only one clear. The buffer will be full after