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

How to achieve java thread closure

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article focuses on "how to achieve java thread closure", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve java thread closure"!

Thread closure

When multiple threads access shared variable data, shared data is not used all the time, so the concept of thread closure is put forward. The so-called thread closure means that the data is enclosed in their own threads and does not need to be synchronized. The main implementations of thread closure are ThreadLocal and local variables.

ThreadLocal

ThreadLocal is a special variable in Java, a thread-level variable, and each thread has its own independent ThreadLocal variable. Its competition condition is completely eliminated and it is absolutely safe in concurrent mode. Usage: ThreadLocal var = new ThreadLocal (); a copy of T is automatically created on each thread, independent of each other and independent of each other. You can use ThreadLocal to store some parameters for use in multiple methods in a thread instead of passing parameters in a method.

Code example:

/ * * Thread closure example * / public class Demo6 {/ * * threadLocal variable, each thread has a copy and does not interfere with each other * / public static ThreadLocal value = new ThreadLocal (); / * * threadlocal test * * @ throws Exception * / public void threadLocalTest () throws Exception {/ / threadlocal thread closure example value.set ("this is set by the main thread") / / main thread setting value String v = value.get (); System.out.println ("value taken by the main thread before thread 1 executes:" + v); new Thread (new Runnable () {@ Override public void run () {String v = value.get ()) System.out.println ("value taken by thread 1:" + v); / / set threadLocal value.set ("this is set by thread 1 456"); v = value.get (); System.out.println ("value taken by thread 1 after reset:" + v) System.out.println ("Thread 1 execution ends");}}) .start (); Thread.sleep (5000L); / / wait for the end of all thread execution v = value.get (); System.out.println ("after thread 1 executes, the value taken by the main thread:" + v) } public static void main (String [] args) throws Exception {new Demo6 () .threadLocalTest ();}}

Output result:

Before thread 1 executes, the main thread gets the value: this is the value set by the main thread 123thread 1: after the null is reset, thread 1 gets the value: this is the value that the main thread gets after thread 1 sets 456 thread 1 execution ends thread 1 execution: this is the 123stack closed set by the main thread

One of the inherent properties of local variables is that they are in the stack of the executing thread in the closed thread, and other threads cannot access the stack.

At this point, I believe that everyone on "how to achieve java thread closure" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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