1 /*
   2  * Copyright (c) 2003, 2018, 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 /*
  26   test
  27   @bug 4828019
  28   @summary Frame/Window deadlock
  29   @author yan@sparc.spb.su: area=
  30   @run applet NonEDT_GUI_Deadlock.html
  31 */
  32 
  33 // Note there is no @ in front of test above.  This is so that the
  34 //  harness will not mistake this file as a test file.  It should
  35 //  only see the html file as a test file. (the harness runs all
  36 //  valid test files, so it would run this test twice if this file
  37 //  were valid as well as the html file.)
  38 // Also, note the area= after Your Name in the author tag.  Here, you
  39 //  should put which functional area the test falls in.  See the
  40 //  AWT-core home page -> test areas and/or -> AWT team  for a list of
  41 //  areas.
  42 // Note also the 'AutomaticAppletTest.html' in the run tag.  This should
  43 //  be changed to the name of the test.
  44 
  45 
  46 /**
  47  * NonEDT_GUI_Deadlock.java
  48  *
  49  * summary:
  50  */
  51 
  52 import java.applet.Applet;
  53 import java.awt.*;
  54 import java.awt.event.*;
  55 import java.net.*;
  56 import java.io.*;
  57 
  58 
  59 //Automated tests should run as applet tests if possible because they
  60 // get their environments cleaned up, including AWT threads, any
  61 // test created threads, and any system resources used by the test
  62 // such as file descriptors.  (This is normally not a problem as
  63 // main tests usually run in a separate VM, however on some platforms
  64 // such as the Mac, separate VMs are not possible and non-applet
  65 // tests will cause problems).  Also, you don't have to worry about
  66 // synchronisation stuff in Applet tests they way you do in main
  67 // tests...
  68 
  69 
  70 public class NonEDT_GUI_Deadlock extends Applet
  71 {
  72     //Declare things used in the test, like buttons and labels here
  73     boolean bOK = false;
  74     Thread badThread = null;
  75 
  76     public void init()
  77     {
  78     }//End  init()
  79 
  80     public void start ()
  81     {
  82         //Get things going.  Request focus, set size, et cetera
  83 
  84         setSize (200,300);
  85         setVisible(true);
  86         validate();
  87 
  88         final Frame theFrame = new Frame("Window test");
  89         theFrame.setSize(240, 200);
  90 
  91         Thread thKiller = new Thread() {
  92            public void run() {
  93               try {
  94                  Thread.sleep( 9000 );
  95               }catch( Exception ex ) {
  96               }
  97               if( !bOK ) {
  98                  // oops,
  99                  //System.out.println("Deadlock!");
 100                  Runtime.getRuntime().halt(0);
 101               }else{
 102                  //System.out.println("Passed ok.");
 103               }
 104            }
 105         };
 106         thKiller.setName("Killer thread");
 107         thKiller.start();
 108         Window w = new TestWindow(theFrame);
 109         theFrame.toBack();
 110         theFrame.setVisible(true);
 111 
 112         theFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
 113         EventQueue.invokeLater(new Runnable() {
 114            public void run() {
 115                bOK = true;
 116            }
 117         });
 118 
 119 
 120 
 121     }// start()
 122     class TestWindow extends Window implements Runnable {
 123 
 124         TestWindow(Frame f) {
 125             super(f);
 126 
 127             //setSize(240, 75);
 128             setLocation(0, 75);
 129 
 130             show();
 131             toFront();
 132 
 133             badThread = new Thread(this);
 134             badThread.setName("Bad Thread");
 135             badThread.start();
 136 
 137         }
 138 
 139         public void paint(Graphics g) {
 140             g.drawString("Deadlock or no deadlock?",20,80);
 141         }
 142 
 143         public void run() {
 144 
 145             long ts = System.currentTimeMillis();
 146 
 147             while (true) {
 148                 if ((System.currentTimeMillis()-ts)>3000) {
 149                     this.setVisible( false );
 150                     dispose();
 151                     break;
 152                 }
 153 
 154                 toFront();
 155                 try {
 156                     Thread.sleep(80);
 157                 } catch (Exception e) {
 158                 }
 159             }
 160         }
 161     }
 162 
 163 
 164 
 165     public static void main(String args[]) {
 166        NonEDT_GUI_Deadlock imt = new NonEDT_GUI_Deadlock();
 167        imt.init();
 168        imt.start();
 169     }
 170 
 171 
 172 }// class NonEDT_GUI_Deadlock