< prev index next >

test/jdk/java/awt/Frame/NonEDT_GUI_DeadlockTest/NonEDT_GUI_Deadlock.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 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  */


  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         //Create instructions for the user here, as well as set up
  79         // the environment -- set the layout manager, add buttons,
  80         // etc.
  81 
  82 
  83         String[] instructions =
  84         {
  85             "This is an AUTOMATIC test",
  86             "simply wait until it is done"
  87         };
  88         Sysout.createDialog( );
  89         Sysout.printInstructions( instructions );
  90 
  91     }//End  init()
  92 
  93     public void start ()
  94     {
  95         //Get things going.  Request focus, set size, et cetera
  96 
  97         setSize (200,300);
  98         setVisible(true);
  99         validate();
 100 
 101         final Frame theFrame = new Frame("Window test");
 102         theFrame.setSize(240, 200);
 103 
 104         Thread thKiller = new Thread() {
 105            public void run() {
 106               try {
 107                  Thread.sleep( 9000 );
 108               }catch( Exception ex ) {
 109               }
 110               if( !bOK ) {
 111                  // oops,
 112                  //Sysout.println("Deadlock!");
 113                  Runtime.getRuntime().halt(0);
 114               }else{
 115                  //Sysout.println("Passed ok.");
 116               }
 117            }
 118         };
 119         thKiller.setName("Killer thread");
 120         thKiller.start();
 121         Window w = new TestWindow(theFrame);
 122         theFrame.toBack();
 123         theFrame.setVisible(true);
 124 
 125         theFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
 126         EventQueue.invokeLater(new Runnable() {
 127            public void run() {
 128                bOK = true;
 129            }
 130         });
 131 
 132 
 133 
 134     }// start()
 135     class TestWindow extends Window implements Runnable {


 166 
 167                 toFront();
 168                 try {
 169                     Thread.sleep(80);
 170                 } catch (Exception e) {
 171                 }
 172             }
 173         }
 174     }
 175 
 176 
 177 
 178     public static void main(String args[]) {
 179        NonEDT_GUI_Deadlock imt = new NonEDT_GUI_Deadlock();
 180        imt.init();
 181        imt.start();
 182     }
 183 
 184 
 185 }// class NonEDT_GUI_Deadlock
 186 
 187 
 188 /****************************************************
 189  Standard Test Machinery
 190  DO NOT modify anything below -- it's a standard
 191   chunk of code whose purpose is to make user
 192   interaction uniform, and thereby make it simpler
 193   to read and understand someone else's test.
 194  ****************************************************/
 195 
 196 /**
 197  This is part of the standard test machinery.
 198  It creates a dialog (with the instructions), and is the interface
 199   for sending text messages to the user.
 200  To print the instructions, send an array of strings to Sysout.createDialog
 201   WithInstructions method.  Put one line of instructions per array entry.
 202  To display a message for the tester to see, simply call Sysout.println
 203   with the string to be displayed.
 204  This mimics System.out.println but works within the test harness as well
 205   as standalone.
 206  */
 207 
 208 class Sysout
 209 {
 210     private static TestDialog dialog;
 211 
 212     public static void createDialogWithInstructions( String[] instructions )
 213     {
 214         dialog = new TestDialog( new Frame(), "Instructions" );
 215         dialog.printInstructions( instructions );
 216         dialog.setVisible(true);
 217         println( "Any messages for the tester will display here." );
 218     }
 219 
 220     public static void createDialog( )
 221     {
 222         dialog = new TestDialog( new Frame(), "Instructions" );
 223         String[] defInstr = { "Instructions will appear here. ", "" } ;
 224         dialog.printInstructions( defInstr );
 225         dialog.setVisible(true);
 226         println( "Any messages for the tester will display here." );
 227     }
 228 
 229 
 230     public static void printInstructions( String[] instructions )
 231     {
 232         dialog.printInstructions( instructions );
 233     }
 234 
 235 
 236     public static void println( String messageIn )
 237     {
 238         dialog.displayMessage( messageIn );
 239     }
 240 
 241 }// Sysout  class
 242 
 243 /**
 244   This is part of the standard test machinery.  It provides a place for the
 245    test instructions to be displayed, and a place for interactive messages
 246    to the user to be displayed.
 247   To have the test instructions displayed, see Sysout.
 248   To have a message to the user be displayed, see Sysout.
 249   Do not call anything in this dialog directly.
 250   */
 251 class TestDialog extends Dialog
 252 {
 253 
 254     TextArea instructionsText;
 255     TextArea messageText;
 256     int maxStringLength = 80;
 257 
 258     //DO NOT call this directly, go through Sysout
 259     public TestDialog( Frame frame, String name )
 260     {
 261         super( frame, name );
 262         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 263         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 264         add( "North", instructionsText );
 265 
 266         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 267         add("Center", messageText);
 268 
 269         pack();
 270 
 271         show();
 272     }// TestDialog()
 273 
 274     //DO NOT call this directly, go through Sysout
 275     public void printInstructions( String[] instructions )
 276     {
 277         //Clear out any current instructions
 278         instructionsText.setText( "" );
 279 
 280         //Go down array of instruction strings
 281 
 282         String printStr, remainingStr;
 283         for( int i=0; i < instructions.length; i++ )
 284         {
 285             //chop up each into pieces maxSringLength long
 286             remainingStr = instructions[ i ];
 287             while( remainingStr.length() > 0 )
 288             {
 289                 //if longer than max then chop off first max chars to print
 290                 if( remainingStr.length() >= maxStringLength )
 291                 {
 292                     //Try to chop on a word boundary
 293                     int posOfSpace = remainingStr.
 294                         lastIndexOf( ' ', maxStringLength - 1 );
 295 
 296                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 297 
 298                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 299                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 300                 }
 301                 //else just print
 302                 else
 303                 {
 304                     printStr = remainingStr;
 305                     remainingStr = "";
 306                 }
 307 
 308                 instructionsText.append( printStr + "\n" );
 309 
 310             }// while
 311 
 312         }// for
 313 
 314     }//printInstructions()
 315 
 316     //DO NOT call this directly, go through Sysout
 317     public void displayMessage( String messageIn )
 318     {
 319         messageText.append( messageIn + "\n" );
 320         System.out.println(messageIn);
 321     }
 322 
 323 }// TestDialog  class
   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  */


  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 {


 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










































































































































< prev index next >