ThreadLocal03.java revision 12657:6ef01bd40ce2
1130561Sobrien/*
2130561Sobrien * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
384865Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
484865Sobrien *
584865Sobrien * This code is free software; you can redistribute it and/or modify it
684865Sobrien * under the terms of the GNU General Public License version 2 only, as
784865Sobrien * published by the Free Software Foundation.
884865Sobrien *
984865Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1084865Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1184865Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1284865Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1384865Sobrien * accompanied this code).
1484865Sobrien *
1584865Sobrien * You should have received a copy of the GNU General Public License version
1684865Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1784865Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1884865Sobrien *
1984865Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2084865Sobrien * or visit www.oracle.com if you need additional information or have any
2184865Sobrien * questions.
2284865Sobrien */
2389857Sobrienpackage org.graalvm.compiler.jtt.threads;
2484865Sobrien
2584865Sobrienimport org.junit.Test;
2684865Sobrien
2784865Sobrienimport org.graalvm.compiler.jtt.JTTTest;
2884865Sobrien
2984865Sobrien/*
3084865Sobrien */
3184865Sobrienpublic class ThreadLocal03 extends JTTTest {
3284865Sobrien
3384865Sobrien    static final ThreadLocal<Integer> local = new ThreadLocal<>();
3484865Sobrien
3584865Sobrien    public static int test(int i) {
3684865Sobrien        int sum = 0;
3784865Sobrien        for (int j = 0; j < i; j++) {
3884865Sobrien            TThread t = new TThread();
3984865Sobrien            t.input = 10 + j;
4084865Sobrien            t.run();
4184865Sobrien            try {
4284865Sobrien                t.join();
4384865Sobrien            } catch (InterruptedException e) {
4484865Sobrien                return -1;
4584865Sobrien            }
4684865Sobrien            sum += t.output;
4784865Sobrien        }
4884865Sobrien        return sum;
4984865Sobrien    }
5084865Sobrien
5184865Sobrien    private static class TThread extends Thread {
5284865Sobrien
5384865Sobrien        int input;
5484865Sobrien        int output;
5584865Sobrien
5684865Sobrien        @Override
5784865Sobrien        public void run() {
5884865Sobrien            local.set(input + 5);
5984865Sobrien            output = local.get();
6084865Sobrien        }
6184865Sobrien    }
6284865Sobrien
6384865Sobrien    @Test
6484865Sobrien    public void run0() throws Throwable {
6584865Sobrien        runTest("test", 0);
6684865Sobrien    }
6784865Sobrien
6884865Sobrien    @Test
6984865Sobrien    public void run1() throws Throwable {
7084865Sobrien        runTest("test", 1);
7184865Sobrien    }
7284865Sobrien
7384865Sobrien    @Test
7484865Sobrien    public void run2() throws Throwable {
7584865Sobrien        runTest("test", 2);
7684865Sobrien    }
7784865Sobrien
7884865Sobrien    @Test
7984865Sobrien    public void run3() throws Throwable {
8084865Sobrien        runTest("test", 3);
8184865Sobrien    }
8284865Sobrien
8384865Sobrien}
8484865Sobrien