Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/awt/AppContext/MultiThread/MultiThreadTest.java
38853 views
/*1* Copyright (c) 2013, 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*/2930import sun.awt.AppContext;3132public class MultiThreadTest {33private static final int NUM_THREADS = 2;3435private static AppContextGetter[] getters = new AppContextGetter[NUM_THREADS];3637public static void main(String[] args) {38createAndStartThreads();39compareAppContexts();40}4142private static void createAndStartThreads() {43ThreadGroup systemGroup = getSystemThreadGroup();44for (int i = 0; i < NUM_THREADS; ++i) {45ThreadGroup tg = new ThreadGroup(systemGroup, "AppContextGetter" + i);46getters[i] = new AppContextGetter(tg);47}4849for (int i = 0; i < NUM_THREADS; ++i) {50getters[i].start();51}5253for (int i = 0; i < NUM_THREADS; ++i) {54try {55getters[i].join();56} catch (InterruptedException e) {57// ignore58}59}60}6162private static ThreadGroup getSystemThreadGroup() {63ThreadGroup currentThreadGroup =64Thread.currentThread().getThreadGroup();65ThreadGroup parentThreadGroup = currentThreadGroup.getParent();66while (parentThreadGroup != null) {67currentThreadGroup = parentThreadGroup;68parentThreadGroup = currentThreadGroup.getParent();69}7071return currentThreadGroup;72}7374private static void compareAppContexts() {75AppContext ctx = getters[0].getAppContext();76for (int i = 1; i < NUM_THREADS; ++i) {77if (!ctx.equals(getters[i].getAppContext())) {78throw new RuntimeException("Unexpected AppContexts difference, could be a race condition");79}80}81}8283private static class AppContextGetter extends Thread {84private AppContext appContext;8586public AppContextGetter(ThreadGroup tg) {87super(tg, tg.getName());88}8990AppContext getAppContext() {91return appContext;92}9394@Override95public void run() {96appContext = AppContext.getAppContext();97}98}99}100101102