Path: blob/master/test/jdk/java/awt/Frame/GetGraphicsStressTest/GetGraphicsStressTest.java
41153 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Frame;24import java.awt.Graphics;25import java.util.concurrent.TimeUnit;2627/**28* @test29* @bug 8235638 823573930* @key headful31*/32public final class GetGraphicsStressTest {3334static volatile Throwable failed;35static volatile long endtime;3637public static void main(final String[] args) throws Exception {38// Catch all uncaught exceptions and treat them as test failure39Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);4041// Will run the test no more than 20 seconds42for (int i = 0; i < 4; i++) {43endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);44test();45}46}4748private static void test() throws Exception {49Frame f = new Frame();50f.setSize(100, 100);51f.setLocationRelativeTo(null);52f.setVisible(true);5354Thread thread1 = new Thread(() -> {55while (!isComplete()) {56f.removeNotify();57f.addNotify();58}59});60Thread thread2 = new Thread(() -> {61while (!isComplete()) {62Graphics g = f.getGraphics();63if (g != null) {64g.dispose();65}66}67});68Thread thread3 = new Thread(() -> {69while (!isComplete()) {70Graphics g = f.getGraphics();71if (g != null) {72g.dispose();73}74}75});76Thread thread4 = new Thread(() -> {77while (!isComplete()) {78Graphics g = f.getGraphics();79if (g != null) {80g.drawLine(0, 0, 4, 4); // just in case...81g.dispose();82}83}84});85thread1.start();86thread2.start();87thread3.start();88thread4.start();89thread1.join();90thread2.join();91thread3.join();92thread4.join();9394f.dispose();95if (failed != null) {96System.err.println("Test failed");97failed.printStackTrace();98throw new RuntimeException(failed);99}100}101102private static boolean isComplete() {103return endtime - System.nanoTime() < 0 || failed != null;104}105}106107108