Path: blob/master/test/jdk/sun/awt/AppContext/MultiThread/MultiThreadTest.java
41155 views
/*1* Copyright (c) 2013, 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 801962326* @summary Tests that AppContext.getAppContext() works correctly in multi-threads scenario.27* @author Leonid Romanov28* @modules java.desktop/sun.awt29*/3031import sun.awt.AppContext;3233public class MultiThreadTest {34private static final int NUM_THREADS = 2;3536private static AppContextGetter[] getters = new AppContextGetter[NUM_THREADS];3738public static void main(String[] args) {39createAndStartThreads();40compareAppContexts();41}4243private static void createAndStartThreads() {44ThreadGroup systemGroup = getSystemThreadGroup();45for (int i = 0; i < NUM_THREADS; ++i) {46ThreadGroup tg = new ThreadGroup(systemGroup, "AppContextGetter" + i);47getters[i] = new AppContextGetter(tg);48}4950for (int i = 0; i < NUM_THREADS; ++i) {51getters[i].start();52}5354for (int i = 0; i < NUM_THREADS; ++i) {55try {56getters[i].join();57} catch (InterruptedException e) {58// ignore59}60}61}6263private static ThreadGroup getSystemThreadGroup() {64ThreadGroup currentThreadGroup =65Thread.currentThread().getThreadGroup();66ThreadGroup parentThreadGroup = currentThreadGroup.getParent();67while (parentThreadGroup != null) {68currentThreadGroup = parentThreadGroup;69parentThreadGroup = currentThreadGroup.getParent();70}7172return currentThreadGroup;73}7475private static void compareAppContexts() {76AppContext ctx = getters[0].getAppContext();77for (int i = 1; i < NUM_THREADS; ++i) {78if (!ctx.equals(getters[i].getAppContext())) {79throw new RuntimeException("Unexpected AppContexts difference, could be a race condition");80}81}82}8384private static class AppContextGetter extends Thread {85private AppContext appContext;8687public AppContextGetter(ThreadGroup tg) {88super(tg, tg.getName());89}9091AppContext getAppContext() {92return appContext;93}9495@Override96public void run() {97appContext = AppContext.getAppContext();98}99}100}101102103