Trending October 2023 # How To Create Javathreadlocal With Sample Code # Suggested November 2023 # Top 11 Popular | Phuhoabeautyspa.com

Trending October 2023 # How To Create Javathreadlocal With Sample Code # Suggested November 2023 # Top 11 Popular

You are reading the article How To Create Javathreadlocal With Sample Code updated in October 2023 on the website Phuhoabeautyspa.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How To Create Javathreadlocal With Sample Code

Introduction to Java ThreadLocal How does ThreadLocal class work in Java?

The thread-local variable is provided by java.lang.ThreadLocal class in Java. These variables are different from the normal variables so that they can be accessed by using the get or set method by each thread that is independent, own, and has a copy of the variable, which is initialized. This is basically one of the other ways to achieve the safety of the thread apart from the writing of immutable classes.

Creation of ThreadLocal in Java

The instance of ThreadLocal is created like how an instance of any other java object is created using a new operator. Consider the below syntax to create a ThreadLocal in Java:

private ThreadLocal variable_name = new ThreadLocal(); Get ThreadLocal value in Java

The value stored in the ThreadLocal can be read using its get() method. Consider the below syntax to get a ThreadLocal value in Java:

String variable_name = (String) threadLocal.get(); Set ThreadLocal value in Java

The value to be stored in the ThreadLocal after its creation can be set usingset() method. Consider the below syntax to set a ThreadLocal value in Java:

threadLocal.set("value_that_needs_to_be_set"); Remove ThreadLocal value in Java.

The value set in the ThreadLocal after the creation of it can be removed. The ThreadLocal value can be removed by using ThreadLocal remove() method. Consider the below syntax to remove a ThreadLocal value that is set in Java:

threadLocal.remove(); Examples to Implement Java ThreadLocal

Below are examples mentioned:

Example #1

Code:

public class Demo { public static void main(String[] args) { local.set(100); System.out.println("The number value that was set is = " + local.get()); local.set(90); System.out.println("The number value that was set is = " + local.get()); val.set("Welcome to ThreadLocal"); System.out.println("The string value that was set is = " + val.get()); val.set("Learning is fun"); System.out.println("The string value that was set is = " + val.get()); } }

Output:

Explanation: In the above program, a class called demo is defined. Then the main method is defined. Then an instance of the ThreadLocal is created for a number. Then an instance of the ThreadLocal is created for a string. Then the first number value is set using the set() method. Then the current thread’s value which returns a number is obtained using the get() method. Then the second numerical value is set using the set() method. Then the current thread’s value which returns a number is obtained using the get() method. Then the first string value is set using the set() method. Then the current thread’s value which returns a string is obtained using the get() method. Then the second string value is set using the set() method. Then the current thread’s value which returns a string is obtained using the get() method. The output of the program is shown in the snapshot above.

Example #2

Java program to demonstrate remove() method of ThreadLocal class:

public class Demo { public static void main(String[] args) { local.set(100); System.out.println("The first number value that was set is = " + local.get()); local.set(90); System.out.println("The second number value that was set is = " + local.get()); val.set("Welcome to ThreadLocal"); System.out.println("The first string value that was set is = " + val.get()); val.remove(); System.out.println("The first string value after using remove() method is = " + val.get()); local.remove(); System.out.println("The first number value and second number value after using remove() method is = " + local.get()); } }

Output:

Explanation: In the above program, a class called demo is defined. Then the main method is defined. Then an instance of the ThreadLocal is created for a number. Then an instance of the ThreadLocal is created for a string. Then the first number value is set using the set() method. Then the current thread’s value which returns a number is obtained using the get() method. Then the second numerical value is set using the set() method. Then the current thread’s value which returns a number is obtained using the get() method. Then the first string value is set using the set() method.

Then remove() method is used to remove the first string value that was set using the set() method. Then the current thread’s value is obtained using the get() method, and it returns null because we removed the value that was set using the remove() method. Then remove() method is used to remove the first number value and second number value that was set using the set() method. Then the current thread’s value is obtained using the get() method, and it returns null because we removed the values that were set using the remove() method. The output of the program is shown in the snapshot above.

Advantages of ThreadLocal class in Java

Conclusion

In this tutorial, we understand ThreadLocal class’s concept through definition, working of ThreadLocal class, and their methods through programming examples and their outputs.

Recommended Articles

You're reading How To Create Javathreadlocal With Sample Code

Update the detailed information about How To Create Javathreadlocal With Sample Code on the Phuhoabeautyspa.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!