Java ThreadLocal Example(java中的ThreadLocal例子)

Java ThreadLocal is used to create thread local variables. We know that all threads of an Object share it’s variables, so the variable is not thread safe. We can use synchronization for thread safety but if we want to avoid synchronization, we can use ThreadLocal variables.

Java ThreadLocal

ThreadLocal, Java ThreadLocal

Every thread has it’s own ThreadLocal variable and they can use it’s get() and set() methods to get the default value or change it’s value local to Thread.

ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread.

Java ThreadLocal Example

Here is a small example showing use of ThreadLocal in java program and proving that every thread has it’s own copy of ThreadLocal variable.

ThreadLocalExample.java

Copy
package com.journaldev.threads; import java.text.SimpleDateFormat; import java.util.Random; public class ThreadLocalExample implements Runnable{ // SimpleDateFormat is not thread-safe, so give one to each thread private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){ @Override protected SimpleDateFormat initialValue() { return new SimpleDateFormat("yyyyMMdd HHmm"); } }; public static void main(String[] args) throws InterruptedException { ThreadLocalExample obj = new ThreadLocalExample(); for(int i=0 ; i<10; i++){ Thread t = new Thread(obj, ""+i); Thread.sleep(new Random().nextInt(1000)); t.start(); } } @Override public void run() { System.out.println("Thread Name= "+Thread.currentThread().getName()+" default Formatter = "+formatter.get().toPattern()); try { Thread.sleep(new Random().nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); } //formatter pattern is changed here by thread, but it won't reflect to other threads formatter.set(new SimpleDateFormat()); System.out.println("Thread Name= "+Thread.currentThread().getName()+" formatter = "+formatter.get().toPattern()); } }

Output of the above java ThreadLocal example program is:

Copy
Thread Name= 0 default Formatter = yyyyMMdd HHmm Thread Name= 1 default Formatter = yyyyMMdd HHmm Thread Name= 0 formatter = M/d/yy h:mm a Thread Name= 2 default Formatter = yyyyMMdd HHmm Thread Name= 1 formatter = M/d/yy h:mm a Thread Name= 3 default Formatter = yyyyMMdd HHmm Thread Name= 4 default Formatter = yyyyMMdd HHmm Thread Name= 4 formatter = M/d/yy h:mm a Thread Name= 5 default Formatter = yyyyMMdd HHmm Thread Name= 2 formatter = M/d/yy h:mm a Thread Name= 3 formatter = M/d/yy h:mm a Thread Name= 6 default Formatter = yyyyMMdd HHmm Thread Name= 5 formatter = M/d/yy h:mm a Thread Name= 6 formatter = M/d/yy h:mm a Thread Name= 7 default Formatter = yyyyMMdd HHmm Thread Name= 8 default Formatter = yyyyMMdd HHmm Thread Name= 8 formatter = M/d/yy h:mm a Thread Name= 7 formatter = M/d/yy h:mm a Thread Name= 9 default Formatter = yyyyMMdd HHmm Thread Name= 9 formatter = M/d/yy h:mm a

As you can see from the output that Thread-0 has changed the value of formatter but still thread-2 default formatter is same as the initialized value. You can see the same pattern for other threads too.

Update: ThreadLocal class is extend in Java 8 with a new method withInitial() that takes Supplier functional interface as argument. So we can use lambda expressions to easily create the ThreadLocal instance. For example, above formatter ThreadLocal variable can be defined in one line as below:

Copy
private static final ThreadLocal<SimpleDateFormat> formatter = ThreadLocal.<SimpleDateFormat>withInitial (() -> {return new SimpleDateFormat("yyyyMMdd HHmm");});

If you are new to Java 8 features, please check out Java 8 Features and Java 8 Functional Interfaces.

That’s all for ThreadLocal in java programming.

Reference: API Doc

About Pankaj

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Google Plus, Facebook or Twitter. I would love to hear your thoughts and opinions on my articles directly.

Recently I started creating video tutorials too, so do check out my videos on Youtube.

转载于:https://www.cnblogs.com/jpfss/p/9373185.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值