1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.AWTException;
  25 import java.awt.Frame;
  26 import java.awt.Robot;
  27 
  28 import sun.awt.AppContext;
  29 import sun.awt.SunToolkit;
  30 
  31 /**
  32  * @test
  33  * @bug 8136858
  34  * @modules java.desktop/sun.awt
  35  * @run main/othervm/java.security.policy=java.policy -Djava.security.manager ApplicationThreadsStop
  36  */
  37 public final class ApplicationThreadsStop implements Runnable {
  38 
  39     private static AppContext contextToDispose;
  40     private static Thread thread;
  41 
  42     public static void main(final String[] args) throws Exception {
  43         ThreadGroup tg = new ThreadGroup("TestThreadGroup");
  44         Thread t = new Thread(tg, new ApplicationThreadsStop());
  45         t.start();
  46         t.join();
  47         contextToDispose.dispose();
  48         // wait for appcontext to be destroyed
  49         Thread.sleep(10000);
  50         if(thread.isAlive()){
  51             throw new RuntimeException("Thread is alive");
  52         }
  53     }
  54 
  55     @Override
  56     public void run() {
  57         contextToDispose = SunToolkit.createNewAppContext();
  58         Frame f = new Frame();
  59         f.setSize(300, 300);
  60         f.setLocationRelativeTo(null);
  61         f.setVisible(true);
  62         thread = new Thread(() -> {
  63             while(true);
  64         });
  65         thread.start();
  66         sync();
  67     }
  68 
  69     private static void sync() {
  70         try {
  71             new Robot().waitForIdle();
  72         } catch (AWTException e) {
  73             throw new RuntimeException(e);
  74         }
  75     }
  76 }