1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 import java.lang.ref.*; 28 import java.util.Objects; 29 import java.util.concurrent.atomic.AtomicInteger; 30 import java.util.function.Supplier; 31 32 /** 33 * This class provides thread-local variables. These variables differ from 34 * their normal counterparts in that each thread that accesses one (via its 35 * {@code get} or {@code set} method) has its own, independently initialized 36 * copy of the variable. {@code ThreadLocal} instances are typically private 37 * static fields in classes that wish to associate state with a thread (e.g., 38 * a user ID or Transaction ID). 39 * 40 * <p>For example, the class below generates unique identifiers local to each 41 * thread. 42 * A thread's id is assigned the first time it invokes {@code ThreadId.get()} 43 * and remains unchanged on subsequent calls. 44 * <pre> 45 * import java.util.concurrent.atomic.AtomicInteger; 46 * 47 * public class ThreadId { 48 * // Atomic integer containing the next thread ID to be assigned 49 * private static final AtomicInteger nextId = new AtomicInteger(0); 50 * 51 * // Thread local variable containing each thread's ID 52 * private static final ThreadLocal<Integer> threadId = 53 * new ThreadLocal<Integer>() { 54 * @Override protected Integer initialValue() { 55 * return nextId.getAndIncrement(); 56 * } 57 * }; 58 * 59 * // Returns the current thread's unique ID, assigning it if necessary 60 * public static int get() { 61 * return threadId.get(); 62 * } 63 * } 64 * </pre> 65 * <p>Each thread holds an implicit reference to its copy of a thread-local 66 * variable as long as the thread is alive and the {@code ThreadLocal} 67 * instance is accessible; after a thread goes away, all of its copies of 68 * thread-local instances are subject to garbage collection (unless other 69 * references to these copies exist). 70 * 71 * @author Josh Bloch and Doug Lea 72 * @since 1.2 73 */ 74 public class ThreadLocal<T> { 75 /** 76 * ThreadLocals rely on per-thread linear-probe hash maps attached 77 * to each thread (Thread.threadLocals and 78 * inheritableThreadLocals). The ThreadLocal objects act as keys, 79 * searched via threadLocalHashCode. This is a custom hash code 80 * (useful only within ThreadLocalMaps) that eliminates collisions 81 * in the common case where consecutively constructed ThreadLocals 82 * are used by the same threads, while remaining well-behaved in 83 * less common cases. 84 */ 85 private final int threadLocalHashCode = nextHashCode(); 86 87 /** 88 * The next hash code to be given out. Updated atomically. Starts at 89 * zero. 90 */ 91 private static AtomicInteger nextHashCode = 92 new AtomicInteger(); 93 94 /** 95 * The difference between successively generated hash codes - turns 96 * implicit sequential thread-local IDs into near-optimally spread 97 * multiplicative hash values for power-of-two-sized tables. 98 */ 99 private static final int HASH_INCREMENT = 0x61c88647; 100 101 /** 102 * Returns the next hash code. 103 */ 104 private static int nextHashCode() { 105 return nextHashCode.getAndAdd(HASH_INCREMENT); 106 } 107 108 /** 109 * Returns the current thread's "initial value" for this 110 * thread-local variable. This method will be invoked the first 111 * time a thread accesses the variable with the {@link #get} 112 * method, unless the thread previously invoked the {@link #set} 113 * method, in which case the {@code initialValue} method will not 114 * be invoked for the thread. Normally, this method is invoked at 115 * most once per thread, but it may be invoked again in case of 116 * subsequent invocations of {@link #remove} followed by {@link #get}. 117 * 118 * <p>This implementation simply returns {@code null}; if the 119 * programmer desires thread-local variables to have an initial 120 * value other than {@code null}, {@code ThreadLocal} must be 121 * subclassed, and this method overridden. Typically, an 122 * anonymous inner class will be used. 123 * 124 * @return the initial value for this thread-local 125 */ 126 protected T initialValue() { 127 return null; 128 } 129 130 /** 131 * Creates a thread local variable. The initial value of the variable is 132 * determined by invoking the {@code get} method on the {@code Supplier}. 133 * 134 * @param <S> the type of the thread local's value 135 * @param supplier the supplier to be used to determine the initial value 136 * @return a new thread local variable 137 * @throws NullPointerException if the specified supplier is null 138 * @since 1.8 139 */ 140 public static <S> ThreadLocal<S> withInitial(Supplier<? extends S> supplier) { 141 return new SuppliedThreadLocal<>(supplier); 142 } 143 144 /** 145 * Creates a thread local variable. 146 * @see #withInitial(java.util.function.Supplier) 147 */ 148 public ThreadLocal() { 149 } 150 151 /** 152 * Returns the value in the current thread's copy of this 153 * thread-local variable. If the variable has no value for the 154 * current thread, it is first initialized to the value returned 155 * by an invocation of the {@link #initialValue} method. 156 * 157 * @return the current thread's value of this thread-local 158 */ 159 public T get() { 160 Thread t = Thread.currentThread(); 161 ThreadLocalMap map = getMap(t); 162 if (map != null) { 163 ThreadLocalMap.Entry e = map.getEntry(this); 164 if (e != null) { 165 @SuppressWarnings("unchecked") 166 T result = (T)e.value; 167 return result; 168 } 169 } 170 return setInitialValue(); 171 } 172 173 /** 174 * Variant of set() to establish initialValue. Used instead 175 * of set() in case user has overridden the set() method. 176 * 177 * @return the initial value 178 */ 179 private T setInitialValue() { 180 T value = initialValue(); 181 Thread t = Thread.currentThread(); 182 ThreadLocalMap map = getMap(t); 183 if (map != null) 184 map.set(this, value); 185 else 186 createMap(t, value); 187 return value; 188 } 189 190 /** 191 * Sets the current thread's copy of this thread-local variable 192 * to the specified value. Most subclasses will have no need to 193 * override this method, relying solely on the {@link #initialValue} 194 * method to set the values of thread-locals. 195 * 196 * @param value the value to be stored in the current thread's copy of 197 * this thread-local. 198 */ 199 public void set(T value) { 200 Thread t = Thread.currentThread(); 201 ThreadLocalMap map = getMap(t); 202 if (map != null) 203 map.set(this, value); 204 else 205 createMap(t, value); 206 } 207 208 /** 209 * Removes the current thread's value for this thread-local 210 * variable. If this thread-local variable is subsequently 211 * {@linkplain #get read} by the current thread, its value will be 212 * reinitialized by invoking its {@link #initialValue} method, 213 * unless its value is {@linkplain #set set} by the current thread 214 * in the interim. This may result in multiple invocations of the 215 * {@code initialValue} method in the current thread. 216 * 217 * @since 1.5 218 */ 219 public void remove() { 220 ThreadLocalMap m = getMap(Thread.currentThread()); 221 if (m != null) 222 m.remove(this); 223 } 224 225 /** 226 * Get the map associated with a ThreadLocal. Overridden in 227 * InheritableThreadLocal. 228 * 229 * @param t the current thread 230 * @return the map 231 */ 232 ThreadLocalMap getMap(Thread t) { 233 return t.threadLocals; 234 } 235 236 /** 237 * Create the map associated with a ThreadLocal. Overridden in 238 * InheritableThreadLocal. 239 * 240 * @param t the current thread 241 * @param firstValue value for the initial entry of the map 242 */ 243 void createMap(Thread t, T firstValue) { 244 t.threadLocals = new ThreadLocalMap(this, firstValue); 245 } 246 247 /** 248 * Factory method to create map of inherited thread locals. 249 * Designed to be called only from Thread constructor. 250 * 251 * @param parentMap the map associated with parent thread 252 * @return a map containing the parent's inheritable bindings 253 */ 254 static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) { 255 return new ThreadLocalMap(parentMap); 256 } 257 258 /** 259 * Method childValue is visibly defined in subclass 260 * InheritableThreadLocal, but is internally defined here for the 261 * sake of providing createInheritedMap factory method without 262 * needing to subclass the map class in InheritableThreadLocal. 263 * This technique is preferable to the alternative of embedding 264 * instanceof tests in methods. 265 */ 266 T childValue(T parentValue) { 267 throw new UnsupportedOperationException(); 268 } 269 270 /** 271 * An extension of ThreadLocal that obtains its initial value from 272 * the specified {@code Supplier}. 273 */ 274 static final class SuppliedThreadLocal<T> extends ThreadLocal<T> { 275 276 private final Supplier<? extends T> supplier; 277 278 SuppliedThreadLocal(Supplier<? extends T> supplier) { 279 this.supplier = Objects.requireNonNull(supplier); 280 } 281 282 @Override 283 protected T initialValue() { 284 return supplier.get(); 285 } 286 } 287 288 /** 289 * ThreadLocalMap is a customized hash map suitable only for 290 * maintaining thread local values. No operations are exported 291 * outside of the ThreadLocal class. The class is package private to 292 * allow declaration of fields in class Thread. To help deal with 293 * very large and long-lived usages, the hash table entries use 294 * WeakReferences for keys. However, since reference queues are not 295 * used, stale entries are guaranteed to be removed only when 296 * the table starts running out of space. 297 */ 298 static class ThreadLocalMap { 299 300 /** 301 * The entries in this hash map extend WeakReference, using 302 * its main ref field as the key (which is always a 303 * ThreadLocal object). Note that null keys (i.e. entry.get() 304 * == null) mean that the key is no longer referenced, so the 305 * entry can be expunged from table. Such entries are referred to 306 * as "stale entries" in the code that follows. 307 */ 308 static class Entry extends WeakReference<ThreadLocal<?>> { 309 /** The value associated with this ThreadLocal. */ 310 Object value; 311 312 Entry(ThreadLocal<?> k, Object v) { 313 super(k); 314 value = v; 315 } 316 } 317 318 /** 319 * The initial capacity -- MUST be a power of two. 320 */ 321 private static final int INITIAL_CAPACITY = 16; 322 323 /** 324 * The table, resized as necessary. 325 * table.length MUST always be a power of two. 326 */ 327 private Entry[] table; 328 329 /** 330 * The number of entries in the table. 331 */ 332 private int size = 0; 333 334 /** 335 * The next size value at which to resize. 336 */ 337 private int threshold; // Default to 0 338 339 /** 340 * Set the resize threshold to maintain at worst a 2/3 load factor. 341 */ 342 private void setThreshold(int len) { 343 threshold = len * 2 / 3; 344 } 345 346 /** 347 * Increment i modulo len. 348 */ 349 private static int nextIndex(int i, int len) { 350 return ((i + 1 < len) ? i + 1 : 0); 351 } 352 353 /** 354 * Decrement i modulo len. 355 */ 356 private static int prevIndex(int i, int len) { 357 return ((i - 1 >= 0) ? i - 1 : len - 1); 358 } 359 360 /** 361 * Construct a new map initially containing (firstKey, firstValue). 362 * ThreadLocalMaps are constructed lazily, so we only create 363 * one when we have at least one entry to put in it. 364 */ 365 ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) { 366 table = new Entry[INITIAL_CAPACITY]; 367 int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1); 368 table[i] = new Entry(firstKey, firstValue); 369 size = 1; 370 setThreshold(INITIAL_CAPACITY); 371 } 372 373 /** 374 * Construct a new map including all Inheritable ThreadLocals 375 * from given parent map. Called only by createInheritedMap. 376 * 377 * @param parentMap the map associated with parent thread. 378 */ 379 private ThreadLocalMap(ThreadLocalMap parentMap) { 380 Entry[] parentTable = parentMap.table; 381 int len = parentTable.length; 382 setThreshold(len); 383 table = new Entry[len]; 384 385 for (Entry e : parentTable) { 386 if (e != null) { 387 @SuppressWarnings("unchecked") 388 ThreadLocal<Object> key = (ThreadLocal<Object>) e.get(); 389 if (key != null) { 390 Object value = key.childValue(e.value); 391 Entry c = new Entry(key, value); 392 int h = key.threadLocalHashCode & (len - 1); 393 while (table[h] != null) 394 h = nextIndex(h, len); 395 table[h] = c; 396 size++; 397 } 398 } 399 } 400 } 401 402 /** 403 * Get the entry associated with key. This method 404 * itself handles only the fast path: a direct hit of existing 405 * key. It otherwise relays to getEntryAfterMiss. This is 406 * designed to maximize performance for direct hits, in part 407 * by making this method readily inlinable. 408 * 409 * @param key the thread local object 410 * @return the entry associated with key, or null if no such 411 */ 412 private Entry getEntry(ThreadLocal<?> key) { 413 int i = key.threadLocalHashCode & (table.length - 1); 414 Entry e = table[i]; 415 if (e != null && e.get() == key) 416 return e; 417 else 418 return getEntryAfterMiss(key, i, e); 419 } 420 421 /** 422 * Version of getEntry method for use when key is not found in 423 * its direct hash slot. 424 * 425 * @param key the thread local object 426 * @param i the table index for key's hash code 427 * @param e the entry at table[i] 428 * @return the entry associated with key, or null if no such 429 */ 430 private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) { 431 Entry[] tab = table; 432 int len = tab.length; 433 434 while (e != null) { 435 ThreadLocal<?> k = e.get(); 436 if (k == key) 437 return e; 438 if (k == null) 439 expungeStaleEntry(i); 440 else 441 i = nextIndex(i, len); 442 e = tab[i]; 443 } 444 return null; 445 } 446 447 /** 448 * Set the value associated with key. 449 * 450 * @param key the thread local object 451 * @param value the value to be set 452 */ 453 private void set(ThreadLocal<?> key, Object value) { 454 455 // We don't use a fast path as with get() because it is at 456 // least as common to use set() to create new entries as 457 // it is to replace existing ones, in which case, a fast 458 // path would fail more often than not. 459 460 Entry[] tab = table; 461 int len = tab.length; 462 int i = key.threadLocalHashCode & (len-1); 463 464 for (Entry e = tab[i]; 465 e != null; 466 e = tab[i = nextIndex(i, len)]) { 467 ThreadLocal<?> k = e.get(); 468 469 if (k == key) { 470 e.value = value; 471 return; 472 } 473 474 if (k == null) { 475 replaceStaleEntry(key, value, i); 476 return; 477 } 478 } 479 480 tab[i] = new Entry(key, value); 481 int sz = ++size; 482 if (!cleanSomeSlots(i, sz) && sz >= threshold) 483 rehash(); 484 } 485 486 /** 487 * Remove the entry for key. 488 */ 489 private void remove(ThreadLocal<?> key) { 490 Entry[] tab = table; 491 int len = tab.length; 492 int i = key.threadLocalHashCode & (len-1); 493 for (Entry e = tab[i]; 494 e != null; 495 e = tab[i = nextIndex(i, len)]) { 496 if (e.get() == key) { 497 e.clear(); 498 expungeStaleEntry(i); 499 return; 500 } 501 } 502 } 503 504 /** 505 * Replace a stale entry encountered during a set operation 506 * with an entry for the specified key. The value passed in 507 * the value parameter is stored in the entry, whether or not 508 * an entry already exists for the specified key. 509 * 510 * As a side effect, this method expunges all stale entries in the 511 * "run" containing the stale entry. (A run is a sequence of entries 512 * between two null slots.) 513 * 514 * @param key the key 515 * @param value the value to be associated with key 516 * @param staleSlot index of the first stale entry encountered while 517 * searching for key. 518 */ 519 private void replaceStaleEntry(ThreadLocal<?> key, Object value, 520 int staleSlot) { 521 Entry[] tab = table; 522 int len = tab.length; 523 Entry e; 524 525 // Back up to check for prior stale entry in current run. 526 // We clean out whole runs at a time to avoid continual 527 // incremental rehashing due to garbage collector freeing 528 // up refs in bunches (i.e., whenever the collector runs). 529 int slotToExpunge = staleSlot; 530 for (int i = prevIndex(staleSlot, len); 531 (e = tab[i]) != null; 532 i = prevIndex(i, len)) 533 if (e.get() == null) 534 slotToExpunge = i; 535 536 // Find either the key or trailing null slot of run, whichever 537 // occurs first 538 for (int i = nextIndex(staleSlot, len); 539 (e = tab[i]) != null; 540 i = nextIndex(i, len)) { 541 ThreadLocal<?> k = e.get(); 542 543 // If we find key, then we need to swap it 544 // with the stale entry to maintain hash table order. 545 // The newly stale slot, or any other stale slot 546 // encountered above it, can then be sent to expungeStaleEntry 547 // to remove or rehash all of the other entries in run. 548 if (k == key) { 549 e.value = value; 550 551 tab[i] = tab[staleSlot]; 552 tab[staleSlot] = e; 553 554 // Start expunge at preceding stale entry if it exists 555 if (slotToExpunge == staleSlot) 556 slotToExpunge = i; 557 cleanSomeSlots(expungeStaleEntry(slotToExpunge), len); 558 return; 559 } 560 561 // If we didn't find stale entry on backward scan, the 562 // first stale entry seen while scanning for key is the 563 // first still present in the run. 564 if (k == null && slotToExpunge == staleSlot) 565 slotToExpunge = i; 566 } 567 568 // If key not found, put new entry in stale slot 569 tab[staleSlot].value = null; 570 tab[staleSlot] = new Entry(key, value); 571 572 // If there are any other stale entries in run, expunge them 573 if (slotToExpunge != staleSlot) 574 cleanSomeSlots(expungeStaleEntry(slotToExpunge), len); 575 } 576 577 /** 578 * Expunge a stale entry by rehashing any possibly colliding entries 579 * lying between staleSlot and the next null slot. This also expunges 580 * any other stale entries encountered before the trailing null. See 581 * Knuth, Section 6.4 582 * 583 * @param staleSlot index of slot known to have null key 584 * @return the index of the next null slot after staleSlot 585 * (all between staleSlot and this slot will have been checked 586 * for expunging). 587 */ 588 private int expungeStaleEntry(int staleSlot) { 589 Entry[] tab = table; 590 int len = tab.length; 591 592 // expunge entry at staleSlot 593 tab[staleSlot].value = null; 594 tab[staleSlot] = null; 595 size--; 596 597 // Rehash until we encounter null 598 Entry e; 599 int i; 600 for (i = nextIndex(staleSlot, len); 601 (e = tab[i]) != null; 602 i = nextIndex(i, len)) { 603 ThreadLocal<?> k = e.get(); 604 if (k == null) { 605 e.value = null; 606 tab[i] = null; 607 size--; 608 } else { 609 int h = k.threadLocalHashCode & (len - 1); 610 if (h != i) { 611 tab[i] = null; 612 613 // Unlike Knuth 6.4 Algorithm R, we must scan until 614 // null because multiple entries could have been stale. 615 while (tab[h] != null) 616 h = nextIndex(h, len); 617 tab[h] = e; 618 } 619 } 620 } 621 return i; 622 } 623 624 /** 625 * Heuristically scan some cells looking for stale entries. 626 * This is invoked when either a new element is added, or 627 * another stale one has been expunged. It performs a 628 * logarithmic number of scans, as a balance between no 629 * scanning (fast but retains garbage) and a number of scans 630 * proportional to number of elements, that would find all 631 * garbage but would cause some insertions to take O(n) time. 632 * 633 * @param i a position known NOT to hold a stale entry. The 634 * scan starts at the element after i. 635 * 636 * @param n scan control: {@code log2(n)} cells are scanned, 637 * unless a stale entry is found, in which case 638 * {@code log2(table.length)-1} additional cells are scanned. 639 * When called from insertions, this parameter is the number 640 * of elements, but when from replaceStaleEntry, it is the 641 * table length. (Note: all this could be changed to be either 642 * more or less aggressive by weighting n instead of just 643 * using straight log n. But this version is simple, fast, and 644 * seems to work well.) 645 * 646 * @return true if any stale entries have been removed. 647 */ 648 private boolean cleanSomeSlots(int i, int n) { 649 boolean removed = false; 650 Entry[] tab = table; 651 int len = tab.length; 652 do { 653 i = nextIndex(i, len); 654 Entry e = tab[i]; 655 if (e != null && e.get() == null) { 656 n = len; 657 removed = true; 658 i = expungeStaleEntry(i); 659 } 660 } while ( (n >>>= 1) != 0); 661 return removed; 662 } 663 664 /** 665 * Re-pack and/or re-size the table. First scan the entire 666 * table removing stale entries. If this doesn't sufficiently 667 * shrink the size of the table, double the table size. 668 */ 669 private void rehash() { 670 expungeStaleEntries(); 671 672 // Use lower threshold for doubling to avoid hysteresis 673 if (size >= threshold - threshold / 4) 674 resize(); 675 } 676 677 /** 678 * Double the capacity of the table. 679 */ 680 private void resize() { 681 Entry[] oldTab = table; 682 int oldLen = oldTab.length; 683 int newLen = oldLen * 2; 684 Entry[] newTab = new Entry[newLen]; 685 int count = 0; 686 687 for (Entry e : oldTab) { 688 if (e != null) { 689 ThreadLocal<?> k = e.get(); 690 if (k == null) { 691 e.value = null; // Help the GC 692 } else { 693 int h = k.threadLocalHashCode & (newLen - 1); 694 while (newTab[h] != null) 695 h = nextIndex(h, newLen); 696 newTab[h] = e; 697 count++; 698 } 699 } 700 } 701 702 setThreshold(newLen); 703 size = count; 704 table = newTab; 705 } 706 707 /** 708 * Expunge all stale entries in the table. 709 */ 710 private void expungeStaleEntries() { 711 Entry[] tab = table; 712 int len = tab.length; 713 for (int j = 0; j < len; j++) { 714 Entry e = tab[j]; 715 if (e != null && e.get() == null) 716 expungeStaleEntry(j); 717 } 718 } 719 } 720 }