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 /*
  25   test
  26   @bug        8081485
  27   @summary    tests that a program terminates automatically after EventQueue.push()
  28   @author     Anton Nashatyrev : area=toolkit
  29 */
  30 
  31 import sun.awt.AWTAutoShutdown;
  32 
  33 import java.awt.*;
  34 import java.util.EmptyStackException;
  35 
  36 public class EventQueuePushAutoshutdown implements Runnable {
  37     private volatile int status = 2;
  38 
  39     public EventQueuePushAutoshutdown() throws Exception {
  40         Runtime.getRuntime().addShutdownHook(new Thread(this));
  41         Thread thread = new Thread() {
  42             @Override
  43             public void run() {
  44                 status = 0;
  45                 try {
  46                     Thread.sleep(10000);
  47                 } catch (InterruptedException e) {
  48                     e.printStackTrace();
  49                 } finally {
  50                     status = 1;
  51                     System.exit(status);
  52                 }
  53             }
  54         };
  55         thread.setDaemon(true);
  56         thread.start();
  57 
  58         System.setProperty("java.awt.headless", "true");
  59         final EventQueue systemQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
  60         systemQueue.push(new EventQueue());
  61         EventQueue.invokeAndWait(new Runnable() {
  62             @Override
  63             public void run() {
  64                 System.out.println("Activated EDT");
  65             }
  66         });
  67         System.out.println("After EDT activation");
  68     }
  69 
  70     public static void main(String[] args) throws Exception  {
  71         new EventQueuePushAutoshutdown();
  72     }
  73 
  74     @Override
  75     public void run() {
  76         Runtime.getRuntime().halt(status);
  77     }
  78 }