jdk/test/sun/awt/AppContext/MultiThread/MultiThreadTest.java
author yan
Tue, 23 Jun 2015 11:59:27 +0300
changeset 31448 1066345d2a8a
parent 21242 dd191df75e13
permissions -rw-r--r--
8076468: Add @modules to tests in jdk_desktop test group Reviewed-by: yan, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21242
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
     1
/*
31448
1066345d2a8a 8076468: Add @modules to tests in jdk_desktop test group
yan
parents: 21242
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
21242
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
     4
 *
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
     8
 *
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    13
 * accompanied this code).
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    14
 *
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    18
 *
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    21
 * questions.
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    22
 */
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    23
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    24
/*
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    25
 * @test
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    26
 * @bug 8019623
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    27
 * @summary Tests that AppContext.getAppContext() works correctly in multi-threads scenario.
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    28
 * @author Leonid Romanov
31448
1066345d2a8a 8076468: Add @modules to tests in jdk_desktop test group
yan
parents: 21242
diff changeset
    29
 * @modules java.desktop/sun.awt
21242
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    30
 */
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    31
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    32
import sun.awt.AppContext;
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    33
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    34
public class MultiThreadTest {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    35
    private static final int NUM_THREADS = 2;
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    36
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    37
    private static AppContextGetter[] getters = new AppContextGetter[NUM_THREADS];
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    38
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    39
    public static void main(String[] args) {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    40
        createAndStartThreads();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    41
        compareAppContexts();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    42
    }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    43
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    44
    private static void createAndStartThreads() {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    45
        ThreadGroup systemGroup = getSystemThreadGroup();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    46
        for (int i = 0; i < NUM_THREADS; ++i) {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    47
            ThreadGroup tg = new ThreadGroup(systemGroup, "AppContextGetter" + i);
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    48
            getters[i] = new AppContextGetter(tg);
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    49
        }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    50
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    51
        for (int i = 0; i < NUM_THREADS; ++i) {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    52
            getters[i].start();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    53
        }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    54
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    55
        for (int i = 0; i < NUM_THREADS; ++i) {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    56
            try {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    57
                getters[i].join();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    58
            } catch (InterruptedException e) {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    59
                // ignore
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    60
            }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    61
        }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    62
    }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    63
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    64
    private static ThreadGroup getSystemThreadGroup() {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    65
        ThreadGroup currentThreadGroup =
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    66
                Thread.currentThread().getThreadGroup();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    67
        ThreadGroup parentThreadGroup = currentThreadGroup.getParent();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    68
        while (parentThreadGroup != null) {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    69
            currentThreadGroup = parentThreadGroup;
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    70
            parentThreadGroup = currentThreadGroup.getParent();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    71
        }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    72
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    73
        return currentThreadGroup;
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    74
    }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    75
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    76
    private static void compareAppContexts() {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    77
        AppContext ctx = getters[0].getAppContext();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    78
        for (int i = 1; i < NUM_THREADS; ++i) {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    79
            if (!ctx.equals(getters[i].getAppContext())) {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    80
                throw new RuntimeException("Unexpected AppContexts difference, could be a race condition");
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    81
            }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    82
        }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    83
    }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    84
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    85
    private static class AppContextGetter extends Thread {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    86
        private AppContext appContext;
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    87
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    88
        public AppContextGetter(ThreadGroup tg) {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    89
            super(tg, tg.getName());
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    90
        }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    91
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    92
        AppContext getAppContext() {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    93
            return appContext;
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    94
        }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    95
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    96
        @Override
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    97
        public void run() {
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    98
            appContext = AppContext.getAppContext();
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
    99
        }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
   100
    }
dd191df75e13 8019623: Lack of synchronization in AppContext.getAppContext()
leonidr
parents:
diff changeset
   101
}