免费A级毛片无码专区网站-成人国产精品视频一区二区-啊 日出水了 用力乖乖在线-国产黑色丝袜在线观看下-天天操美女夜夜操美女-日韩网站在线观看中文字幕-AV高清hd片XXX国产-亚洲av中文字字幕乱码综合-搬开女人下面使劲插视频

ThreadLocal的介紹與運用

ThreadLocal全面解析學(xué)習(xí)目標(biāo)

  • 了解ThreadLocal的介紹
  • 掌握ThreadLocal的運用場景
  • 了解ThreadLocal的內(nèi)部結(jié)構(gòu)
  • 了解ThreadLocal的核心方法源碼
  • 了解ThreadLocalMap的源碼
1. ThreadLocal介紹1.1 官方介紹/** * This class provides thread-local variables.These variables differ from * their normal counterparts in that each thread that accesses one (via its * {@code get} or {@code set} method) has its own, independently initialized * copy of the variable.{@code ThreadLocal} instances are typically private * static fields in classes that wish to associate state with a thread (e.g., * a user ID or Transaction ID). * * <p>For example, the class below generates unique identifiers local to each * thread. * A thread's id is assigned the first time it invokes {@code ThreadId.get()} * and remains unchanged on subsequent calls. * <pre> * import java.util.concurrent.atomic.AtomicInteger; * * public class ThreadId { *// Atomic integer containing the next thread ID to be assigned *private static final AtomicInteger nextId = new AtomicInteger(0); * *// Thread local variable containing each thread's ID *private static final ThreadLocal&lt;Integer&gt; threadId = *new ThreadLocal&lt;Integer&gt;() { *@Override protected Integer initialValue() { *return nextId.getAndIncrement(); *} *}; * *// Returns the current thread's unique ID, assigning it if necessary *public static int get() { *return threadId.get(); *} * } * </pre> * <p>Each thread holds an implicit reference to its copy of a thread-local * variable as long as the thread is alive and the {@code ThreadLocal} * instance is accessible; after a thread goes away, all of its copies of * thread-local instances are subject to garbage collection (unless other * references to these copies exist). * * @authorJosh Bloch and Doug Lea * @since1.2 */public class ThreadLocal<T> {...? 從Java官方文檔中的描述:ThreadLocal類用來提供線程內(nèi)部的局部變量 。這種變量在多線程環(huán)境下訪問(通過get和set方法訪問)時能保證各個線程的變量相對獨立于其他線程內(nèi)的變量 。ThreadLocal實例通常來說都是private static類型的 , 用于關(guān)聯(lián)線程和線程上下文 。
我們可以得知 ThreadLocal 的作用是:提供線程內(nèi)的局部變量 , 不同的線程之間不會相互干擾 , 這種變量在線程的生命周期內(nèi)起作用 , 減少同一個線程內(nèi)多個函數(shù)或組件之間一些公共變量傳遞的復(fù)雜度 。總結(jié):1. 線程并發(fā): 在多線程并發(fā)的場景下2. 傳遞數(shù)據(jù): 我們可以通過ThreadLocal在同一線程 , 不同組件中傳遞公共變量3. 線程隔離: 每個線程的變量都是獨立的 , 不會相互影響1.2 基本使用1.2.1 常用方法? 在使用之前,我們先來認(rèn)識幾個ThreadLocal的常用方法
方法聲明描述ThreadLocal()創(chuàng)建ThreadLocal對象public void set( T value)設(shè)置當(dāng)前線程綁定的局部變量public T get()獲取當(dāng)前線程綁定的局部變量public void remove()移除當(dāng)前線程綁定的局部變量1.2.2 使用案例我們來看下面這個案例public class MyDemo {private String content;private String getContent() {return content;}private void setContent(String content) {this.content = content;}public static void main(String[] args) {MyDemo demo = new MyDemo();for (int i = 0; i < 5; i++) {Thread thread = new Thread(new Runnable() {@Overridepublic void run() {demo.setContent(Thread.currentThread().getName() + "的數(shù)據(jù)");System.out.println("-----------------------");System.out.println(Thread.currentThread().getName() + "--->" + demo.getContent());}});thread.setName("線程" + i);thread.start();}}}打印結(jié)果:
ThreadLocal的介紹與運用

文章插圖
? 從結(jié)果可以看出多個線程在訪問同一個變量的時候出現(xiàn)的異常 , 線程間的數(shù)據(jù)沒有隔離 。下面我們來看下采用 ThreadLocal 的方式來解決這個問題的例子 。

經(jīng)驗總結(jié)擴(kuò)展閱讀