![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
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