Path: blob/master/test/jdk/java/lang/ThreadLocal/ThreadLocalSupplierTest.java
41149 views
/*1* Copyright (c) 2012, 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*/2223import java.util.concurrent.atomic.AtomicInteger;24import java.util.function.Supplier;25import org.testng.annotations.Test;26import static org.testng.Assert.*;2728/**29* @test30* @run testng ThreadLocalSupplierTest31* @summary tests ThreadLocal.withInitial(<Supplier>).32* Adapted from java.lang.Basic functional test of ThreadLocal33*34* @author Jim Gish <[email protected]>35*/36@Test37public class ThreadLocalSupplierTest {3839static final class IntegerSupplier implements Supplier<Integer> {4041private final AtomicInteger supply = new AtomicInteger(0);4243@Override44public Integer get() {45return supply.getAndIncrement();46}4748public int numCalls() {49return supply.intValue();50}51}5253static IntegerSupplier theSupply = new IntegerSupplier();5455static final class MyThreadLocal extends ThreadLocal<Integer> {5657private final ThreadLocal<Integer> delegate;5859public volatile boolean everCalled;6061public MyThreadLocal(Supplier<Integer> supplier) {62delegate = ThreadLocal.<Integer>withInitial(supplier);63}6465@Override66public Integer get() {67return delegate.get();68}6970@Override71protected synchronized Integer initialValue() {72// this should never be called since we are using the factory instead73everCalled = true;74return null;75}76}7778/**79* Our one and only ThreadLocal from which we get thread ids using a80* supplier which simply increments a counter on each call of get().81*/82static MyThreadLocal threadLocal = new MyThreadLocal(theSupply);8384public void testMultiThread() throws Exception {85final int threadCount = 500;86final Thread th[] = new Thread[threadCount];87final boolean visited[] = new boolean[threadCount];8889// Create and start the threads90for (int i = 0; i < threadCount; i++) {91th[i] = new Thread() {92@Override93public void run() {94final int threadId = threadLocal.get();95assertFalse(visited[threadId], "visited[" + threadId + "]=" + visited[threadId]);96visited[threadId] = true;97// check the get() again98final int secondCheckThreadId = threadLocal.get();99assertEquals(secondCheckThreadId, threadId);100}101};102th[i].start();103}104105// Wait for the threads to finish106for (int i = 0; i < threadCount; i++) {107th[i].join();108}109110assertEquals(theSupply.numCalls(), threadCount);111// make sure the provided initialValue() has not been called112assertFalse(threadLocal.everCalled);113// Check results114for (int i = 0; i < threadCount; i++) {115assertTrue(visited[i], "visited[" + i + "]=" + visited[i]);116}117}118119public void testSimple() {120final String expected = "OneWithEverything";121final ThreadLocal<String> threadLocal = ThreadLocal.<String>withInitial(() -> expected);122assertEquals(expected, threadLocal.get());123}124}125126127