Java ThreadLocal initialValue() 方法



描述

Java ThreadLocal initialValue() 方法返回此執行緒區域性變數的當前執行緒的初始值

宣告

以下是java.lang.ThreadLocal.initialValue() 方法的宣告

protected T initialValue()

引數

返回值

此方法返回此執行緒區域性的初始值。

異常

示例:使用 ThreadLocal 物件的初始值作為整數

以下示例演示了 Java ThreadLocal initialValue() 方法的用法。我們透過擴充套件 Thread 建立了一個 NewThread 類。在這個類中,我們有一個 ThreadLocal 物件 t,其重寫了 initialValue() 方法,該方法返回例項值並將其減 1。在 run 方法中,我們列印當前執行緒名稱和例項值。

package com.tutorialspoint;

public class ThreadLocalDemo {

   public static void main (String [] args) {

      NewThread t1 = new NewThread("R");
      NewThread t2 = new NewThread("S");

      // this will call run() method
      t1.start();
      t2.start();
   }
}

class NewThread extends Thread {
   private static ThreadLocal t = new ThreadLocal() {
      protected Object initialValue() {
         return Integer.valueOf(n--);
      }
   };

   private static int n = 10;
   NewThread(String name) {
      super(name);
   }

   public void run() {
      for(int i = 0; i < 2; i++){
         System.out.println (getName() + " " + t.get());
      }
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

R 9
R 9
S 10
S 10

示例:使用 ThreadLocal 物件的初始值作為雙精度浮點數

以下示例演示了 Java ThreadLocal initialValue() 方法的用法。我們透過擴充套件 Thread 建立了一個 NewThread 類。在這個類中,我們有一個 ThreadLocal 物件 t,其重寫了 initialValue() 方法,該方法返回例項值並將其減 1。在 run 方法中,我們列印當前執行緒名稱和例項值。

package com.tutorialspoint;

public class ThreadLocalDemo {

   public static void main (String [] args) {

      NewThread t1 = new NewThread("R");
      NewThread t2 = new NewThread("S");

      // this will call run() method
      t1.start();
      t2.start();
   }
}

class NewThread extends Thread {
   private static ThreadLocal t = new ThreadLocal() {
      protected Object initialValue() {
         return Double.valueOf(n--);
      }
   };

   private static double n = 10.0;
   NewThread(String name) {
      super(name);
   }

   public void run() {
      for(int i = 0; i < 2; i++){
         System.out.println (getName() + " " + t.get());
      }
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

S 10.0
R 10.0
S 10.0
R 10.0

示例:使用 ThreadLocal 物件的初始值作為單精度浮點數

以下示例演示了 Java ThreadLocal initialValue() 方法的用法。我們透過擴充套件 Thread 建立了一個 NewThread 類。在這個類中,我們有一個 ThreadLocal 物件 t,其重寫了 initialValue() 方法,該方法返回例項值並將其減 1。在 run 方法中,我們列印當前執行緒名稱和例項值。

package com.tutorialspoint;

public class ThreadLocalDemo {

   public static void main (String [] args) {

      NewThread t1 = new NewThread("R");
      NewThread t2 = new NewThread("S");

      // this will call run() method
      t1.start();
      t2.start();
   }
}

class NewThread extends Thread {
   private static ThreadLocal t = new ThreadLocal() {
      protected Object initialValue() {
         return Float.valueOf(n--);
      }
   };

   private static float n = 10.0f;
   NewThread(String name) {
      super(name);
   }

   public void run() {
      for(int i = 0; i < 2; i++){
         System.out.println (getName() + " " + t.get());
      }
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

R 10.0
R 10.0
S 9.0
S 9.0
java_lang_threadlocal.htm
廣告
© . All rights reserved.