1 /*
   2  * Copyright (c) 2019, 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.Frame;
  25 import java.awt.Graphics;
  26 import java.util.concurrent.TimeUnit;
  27 
  28 /**
  29  * @test
  30  * @bug 8235638
  31  * @key headful
  32  */
  33 public final class GetGraphicsStressTest {
  34 
  35     static volatile Throwable failed;
  36     static volatile long endtime;
  37 
  38     public static void main(final String[] args) throws Exception {
  39         // Catch all uncaught exceptions and treat them as test failure
  40         Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);
  41 
  42         // Will run the test no more than 20 seconds
  43         for (int i = 0; i < 4; i++) {
  44             endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
  45             test();
  46         }
  47     }
  48 
  49     private static void test() throws Exception {
  50         Frame f = new Frame();
  51         f.setSize(100, 100);
  52         f.setLocationRelativeTo(null);
  53         f.setVisible(true);
  54 
  55         Thread thread1 = new Thread(() -> {
  56             while (!isComplete()) {
  57                 f.removeNotify();
  58                 f.addNotify();
  59             }
  60         });
  61         Thread thread2 = new Thread(() -> {
  62             while (!isComplete()) {
  63                 Graphics g = f.getGraphics();
  64                 if (g != null) {
  65                     g.dispose();
  66                 }
  67             }
  68         });
  69         Thread thread3 = new Thread(() -> {
  70             while (!isComplete()) {
  71                 Graphics g = f.getGraphics();
  72                 if (g != null) {
  73                     g.dispose();
  74                 }
  75             }
  76         });
  77         Thread thread4 = new Thread(() -> {
  78             while (!isComplete()) {
  79                 Graphics g = f.getGraphics();
  80                 if (g != null) {
  81                     g.drawLine(0, 0, 4, 4); // just in case...
  82                     g.dispose();
  83                 }
  84             }
  85         });
  86         thread1.start();
  87         thread2.start();
  88         thread3.start();
  89         thread4.start();
  90         thread1.join();
  91         thread2.join();
  92         thread3.join();
  93         thread4.join();
  94 
  95         f.dispose();
  96         if (failed != null) {
  97             System.err.println("Test failed");
  98             failed.printStackTrace();
  99             throw new RuntimeException(failed);
 100         }
 101     }
 102 
 103     private static boolean isComplete() {
 104         return endtime - System.nanoTime() < 0 || failed != null;
 105     }
 106 }