Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is ThreadLocal in java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what ThreadLocal is in java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

A brief introduction

ThreadLocal, commonly known as thread-local variables, is a special thread binding mechanism that binds variables to threads and maintains an independent copy of variables for each thread. With ThreadLocal, you can limit the visible range of objects to the same thread.

Jump out of the misunderstanding

It is important to emphasize that do not make an analogy between ThreadLocal and synchronized, because this comparison is meaningless at all! Sysnchronized is a mutually exclusive synchronization mechanism to ensure correct access to shared resources in a multithreaded environment. And ThreadLocal in essence, nothing more than provides a "thread-level" variable scope, it is a thread-closed (each thread unique variable) technology, more bluntly, ThreadLocal can be understood as the scope of the object is limited to a thread context, so that the scope of variables is "thread-level".

When there is no ThreadLocal, a thread may pass through multiple levels and methods during its life cycle. If an object needs to be called multiple times in this thread cycle and is cross-level (shared within threads), the usual practice is to pass parameters. On the other hand, ThreadLocal binds variables to threads, and within a thread cycle, no matter where you are, you can easily get the object through its get method. The convenience of accessing "thread-level variables" is greatly improved.

Let's take a simple example.

Suppose we want to associate a unique sequence number for each thread, and we need to access this sequence number several times in each thread cycle, then we can use ThreadLocal. (of course, the following example does not fully reflect the cross-level cross-method call, just understand it.)

Package concurrent

Import java.util.concurrent.atomic.AtomicInteger

/ * *

* Created by chengxiao on 2016-12-12.

, /

Public class ThreadLocalDemo {

Public static void main (String [] args) {

For (int iTuno Bandi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report