Path: blob/master/test/jdk/java/awt/EventDispatchThread/EDTShutdownTest/EDTShutdownTest.java
41154 views
/*1* Copyright (c) 2014, 2015, 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*/2223/*24@test25@bug 803169426@summary [macosx] TwentyThousandTest test intermittently hangs27@author Oleg Pekhovskiy28@modules java.desktop/sun.awt29@modules java.desktop/java.awt:open30@run main EDTShutdownTest31*/3233import java.awt.EventQueue;34import java.awt.Toolkit;35import java.lang.reflect.InvocationTargetException;36import java.lang.reflect.Method;37import sun.awt.AWTAccessor;3839public class EDTShutdownTest {4041private static boolean passed = false;4243public static void main(String[] args) {44// Force EDT start with InvocationEvent45EventQueue.invokeLater(() -> {46// EventQueue is empty now47EventQueue queue = Toolkit.getDefaultToolkit()48.getSystemEventQueue();49Thread thread = AWTAccessor.getEventQueueAccessor()50.getDispatchThread(queue);51try {52/*53* Clear EventDispatchThread.doDispatch flag to break message54* loop in EventDispatchThread.pumpEventsForFilter()55*/56Method stopDispatching = thread.getClass()57.getDeclaredMethod("stopDispatching", null);58stopDispatching.setAccessible(true);59stopDispatching.invoke(thread, null);6061/*62* Post another InvocationEvent that must be handled by another63* instance of EDT64*/65EventQueue.invokeLater(() -> {66passed = true;67});68}69catch (InvocationTargetException | NoSuchMethodException70| IllegalAccessException e) {71}72});7374// Wait for EDT shutdown75EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();76Thread thread = AWTAccessor.getEventQueueAccessor()77.getDispatchThread(queue);78try {79thread.join();8081/*82* Wait for another EDT instance to handle the InvocationEvent83* and shutdown84*/85thread = AWTAccessor.getEventQueueAccessor()86.getDispatchThread(queue);87if (thread != null) {88thread.join();89}90}91catch (InterruptedException e) {92}9394if (passed) {95System.out.println("Test PASSED!");96}97else {98throw new RuntimeException("Test FAILED!");99}100}101}102103104