8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread
authorasmotrak
Tue, 23 Aug 2016 10:32:14 -0700
changeset 40534 bc3e5bbcb65e
parent 40533 ec047f91988e
child 40535 bd0d32b35eab
8164592: java/net/MulticastSocket/NoLoopbackPackets.java tests may leave a daemon thread Reviewed-by: clanger, chegar
jdk/test/java/net/MulticastSocket/NoLoopbackPackets.java
--- a/jdk/test/java/net/MulticastSocket/NoLoopbackPackets.java	Tue Aug 23 10:30:16 2016 -0400
+++ b/jdk/test/java/net/MulticastSocket/NoLoopbackPackets.java	Tue Aug 23 10:32:14 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2016, 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
@@ -68,6 +68,7 @@
 
         MulticastSocket msock = null;
         List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
+        Sender sender = null;
         try {
             msock = new MulticastSocket();
             int port = msock.getLocalPort();
@@ -80,9 +81,8 @@
             groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
             groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
 
-            Thread sender = new Thread(new Sender(groups));
-            sender.setDaemon(true); // we want sender to stop when main thread exits
-            sender.start();
+            sender = new Sender(groups);
+            new Thread(sender).start();
 
             // Now try to receive multicast packets. we should not see any of them
             // since we disable loopback mode.
@@ -107,6 +107,9 @@
             }
         } finally {
             if (msock != null) try { msock.close(); } catch (Exception e) {}
+            if (sender != null) {
+                sender.stop();
+            }
         }
 
         if (failedGroups.size() > 0) {
@@ -119,6 +122,7 @@
 
     static class Sender implements Runnable {
         private List<SocketAddress> sendToGroups;
+        private volatile boolean stop;
 
         public Sender(List<SocketAddress> groups) {
             sendToGroups = groups;
@@ -136,7 +140,7 @@
 
                 MulticastSocket msock = new MulticastSocket();
                 msock.setLoopbackMode(true);    // disable loopback mode
-                for (;;) {
+                while (!stop) {
                     for (DatagramPacket packet : packets) {
                         msock.send(packet);
                     }
@@ -147,5 +151,9 @@
                 throw new RuntimeException(e);
             }
         }
+
+        void stop() {
+            stop = true;
+        }
     }
 }