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   @test
  26   @key headful
  27   @bug 4828019
  28   @summary Frame/Window deadlock
  29   @run main/timeout=9999 NonEDT_GUI_Deadlock
  30 */
  31 
  32 import java.awt.*;
  33 
  34 public class NonEDT_GUI_Deadlock {
  35     boolean bOK = false;
  36     Thread badThread = null;
  37 
  38     public void start ()
  39     {
  40         final Frame theFrame = new Frame("Window test");
  41         theFrame.setSize(240, 200);
  42 
  43         Thread thKiller = new Thread() {
  44            public void run() {
  45               try {
  46                  Thread.sleep( 9000 );
  47               }catch( Exception ex ) {
  48               }
  49               if( !bOK ) {
  50                  // oops,
  51                  //System.out.println("Deadlock!");
  52                  Runtime.getRuntime().halt(0);
  53               }else{
  54                  //System.out.println("Passed ok.");
  55               }
  56            }
  57         };
  58         thKiller.setName("Killer thread");
  59         thKiller.start();
  60         Window w = new TestWindow(theFrame);
  61         theFrame.toBack();
  62         theFrame.setVisible(true);
  63 
  64         theFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
  65         EventQueue.invokeLater(new Runnable() {
  66            public void run() {
  67                bOK = true;
  68            }
  69         });
  70 
  71 
  72 
  73     }// start()
  74     class TestWindow extends Window implements Runnable {
  75 
  76         TestWindow(Frame f) {
  77             super(f);
  78 
  79             //setSize(240, 75);
  80             setLocation(0, 75);
  81 
  82             show();
  83             toFront();
  84 
  85             badThread = new Thread(this);
  86             badThread.setName("Bad Thread");
  87             badThread.start();
  88 
  89         }
  90 
  91         public void paint(Graphics g) {
  92             g.drawString("Deadlock or no deadlock?",20,80);
  93         }
  94 
  95         public void run() {
  96 
  97             long ts = System.currentTimeMillis();
  98 
  99             while (true) {
 100                 if ((System.currentTimeMillis()-ts)>3000) {
 101                     this.setVisible( false );
 102                     dispose();
 103                     break;
 104                 }
 105 
 106                 toFront();
 107                 try {
 108                     Thread.sleep(80);
 109                 } catch (Exception e) {
 110                 }
 111             }
 112         }
 113     }
 114 
 115 
 116 
 117     public static void main(String args[]) {
 118        NonEDT_GUI_Deadlock imt = new NonEDT_GUI_Deadlock();
 119        imt.start();
 120     }
 121 
 122 
 123 }// class NonEDT_GUI_Deadlock