Java ThreadLocal initialValue() Method



Description

The Java ThreadLocal initialValue() method returns the current thread's initial value for this thread-local variable.

Declaration

Following is the declaration for java.lang.ThreadLocal.initialValue() method

protected T initialValue()

Parameters

NA

Return Value

This method returns the initial value for this thread-local.

Exception

NA

Example: Using Initial Value of ThreadLocal Object as Integer

The following example shows the usage of Java ThreadLocal initialValue() method. We've created a class NewThread by extending Thread. In this class, we've a ThreadLocal object t with overridden initialValue() method which returns instance value and reduce the same by 1. In run method, we're printing the current thread name and the instance value.

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());
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

R 9
R 9
S 10
S 10

Example: Using Initial Value of ThreadLocal Object as Double

The following example shows the usage of Java ThreadLocal initialValue() method. We've created a class NewThread by extending Thread. In this class, we've a ThreadLocal object t with overridden initialValue() method which returns instance value and reduce the same by 1. In run method, we're printing the current thread name and the instance value.

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());
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

S 10.0
R 10.0
S 10.0
R 10.0

Example: Using Initial Value of ThreadLocal Object as Float

The following example shows the usage of Java ThreadLocal initialValue() method. We've created a class NewThread by extending Thread. In this class, we've a ThreadLocal object t with overridden initialValue() method which returns instance value and reduce the same by 1. In run method, we're printing the current thread name and the instance value.

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());
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

R 10.0
R 10.0
S 9.0
S 9.0
java_lang_threadlocal.htm
Advertisements