<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java `ThreadLocal`示例 > 原文: [https://javatutorial.net/threadlocal-java-example](https://javatutorial.net/threadlocal-java-example) `ThreadLocal`是提供線程局部變量的類,用于實現線程安全。 存儲的數據只能由特定線程訪問。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) `ThreadLocal`擴展了`Object`類,并提供了線程限制,它是局部變量的“一部分”。 ## 創建`ThreadLocal`變量 ```java ThreadLocal threadLocalExample = new ThreadLocal(); ``` 上面代碼中`ThreadLocal`對象的實例化僅需要針對每個線程進行。 就像大多數類一樣,一旦有了`ThreadLocal`的實例,就可以在其上調用方法。 一些方法是: 1. `get()`:返回此線程局部變量的當前線程副本中的值 2. `initialValue()`:返回當前線程局部變量的當前線程初始值 3. `remove()`:從當前線程中刪除當前線程局部變量的值 4. `set(T value)`:將當前線程局部變量的當前線程副本設置為指定值 有關這些方法的更多詳細信息,請訪問原始 [Oracle 文檔](https://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html)。 `ThreadLocal`實例是希望將狀態與線程相關聯的類中的私有靜態字段(在大多數情況下) ## 實現示例 ```java public class ThreadLocalExample { public static class Example implements Runnable { private ThreadLocal<String> example = new ThreadLocal<String>(); // override the run() method that comes from implementing Runnable class @Override public void run() { try { System.out.println("Getting values..."); Thread.sleep(2000); } catch (InterruptedException e) { System.out.println(e); } example.set("Just a random text that will be displayed before the remove function"); System.out.println("Before remove: " + example.get()); example.remove(); System.out.println("After remove: " + example.get()); } } public static void main(String[] args) { /* EXAMPLE THAT DOES NOT HAVE TO DO ANYTHING WITH THE STATIC CLASS ABOVE main*/ ThreadLocal<String> local = new ThreadLocal<String>(); local.set("First"); System.out.println("Value: " + local.get()); local.set("Second"); System.out.println("Value: " + local.get()); local.remove(); System.out.println("Value: " + local.get()); /* NEW EXAMPLE THAT USES THE STATIC CLASS DECLARED ABOVE main */ Example runnable = new Example(); Thread thread = new Thread(runnable); thread.start(); } } ``` **輸出** ```java Value: First Value: Second Value: null Getting values... Before remove: Just a random text that will be displayed before the remove function After remove: null ``` ## 分解 上面的代碼顯示了兩種使它起作用的方法:一種是通過擁有`Runnable`對象并將其傳遞給`Thread`實例,然后覆蓋`run()`方法,或者您可以簡單地創建一個`ThreadLocal`實例并為其設置值,然后 可以獲取或刪除它。 從上面的示例中可以看到,即使它是相同的變量(局部變量),也可以包含不同的值。 在第一種情況下,它包含值`"First"`。 在第二種情況下,它包含值`"Second"`。 對于其他實現,我只顯示了一個線程。 但是,每個線程都是獨立的–意味著,如果您要創建另一個`Thread`實例(例如`thread2`),并對其進行`start()`,它將獨立運行,并且與其他`Thread`實例變量無關。 要進行檢查,可以在靜態類中創建一個`ThreadLocal`實例,然后在重寫的`run()`方法中創建一個隨機數,然后使用`set()`方法將其傳遞給當前線程。 您將看到,如果您在兩個或多個不同的線程上調用它,則它們都將具有不同的值。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看