Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ The Java ThreadLocal     - Linux --- process tracking (Linux)

- iOS9 new feature - stacked view UIStackView (Programming)

- Install OpenGL development environment in Fedora and Ubuntu (Linux)

- When Linux Detailed time zone and common function of time (Linux)

- Linux Network Programming - raw socket instance: MAC Address Scanner (Programming)

- Forwarding module with Apache reverse proxy server (Server)

- LMMS 1.03 install on Ubuntu 14.04 (Linux)

- CentOS modify yum update source (Linux)

- ORA-12537: TNS: connection closed error process (Database)

- PL / SQL data types (Database)

- JavaScript function closures Quick Start (Programming)

- Retro terminal in Linux (Linux)

- Linux basic introductory tutorial ---- simple text processing (Linux)

- Use PDFBox processing PDF documents (Linux)

- Build RubyMine + Ruby On Rails + MySQL development environment under Windows (Server)

- Android Touch message passing mechanism analysis (Programming)

- Linux Basics Tutorial: Combining awk delete data before the specified date hdfs (Linux)

- Ubuntu 12.04 installation DHCP Server (Server)

- Linux bash: scp: command not found the problem (Linux)

- To install Oracle Database Details and FAQ Summary under CentOS (Database)

  The Java ThreadLocal
  Add Date : 2018-11-21      

The easiest way to call multiple threads in the same process, there will be complicated by the problem, to solve this problem is to save the data in the Thread of their structure, that is the role of the ThreadLocal. Usage is as follows:

classJavaBean {staticThreadLocal < Integer > threadLocal = newThreadLocal < Integer > (); publicvoid prepare () {
        threadLocal.set (0);} publicvoid work () {for (int i = 0; i <1000; i ++) {int val = threadLocal.get ();
            val ++;
            threadLocal.set (val);} System.out.println (threadLocal.get ());}} classWorkerextendsThread {JavaBean bean; publicWorker (JavaBean bean) {this.bean = bean;} publicvoid run () {
        bean.prepare ();
        bean.work ();}} publicclassThreadLocalDemo {publicstaticvoid main (String [] args) {JavaBean bean = newJavaBean (); for (int i = 0; i <100; i ++) {newWorker (bean) .start ();} }}
You can see from the output, multiple threads operating with a threadLocal, and the results can not go wrong.

ThreadLocal can be seen as a package is Thread.threadLocals Moreover, in the program is not directly accessible to Thread.threadLocals.


Following is a brief look at the principle of ThreadLocal in Thread is saved in a Map, the type can be considered as Map , where T is the type of data you want to save. When removed from the data, the flow call is:

Thread.currentThread (). ThreadLocals.getEntry (threadLocal) .value
Thus, when a different thread of execution on the same threadLocal get to the different data between threads isolation through "each holding a different Map" to achieve, but the object is actually seen threadLocal KEY , before the operation is to get VALUE. In fact, do yourself a thread-safe data storage solution is also this idea.

Not an ordinary reference stored data used in ThreadLocalMap Rather, the WeakReference do:

staticclassEntryextendsWeakReference < ThreadLocal > {Object value; Entry (ThreadLocal k, Object v) {super (k);
        value = v;}}
So if ThreadLocal is released, then ThreadLocalMap the Entry will be released, it will not cause a memory leak.
- CentOS 6.6 compile and install phpMyAdmin configuration PostgreSQL9.4.1 (Database)
- How to find on Linux and delete duplicate files: FSlint (Linux)
- mysqldump MySQL command-line tool (Database)
- Performance Optimization: Using Ramlog transfer log files to memory (Linux)
- Oracle 11g RMAN virtual private directory (Database)
- PHP interview questions of design patterns (Programming)
- Linux Firewall IPCop Profile (Linux)
- Java Access Control (Programming)
- Distributed transaction management Spring declarative transactions (Programming)
- Ten best plug surge Emacs Productivity (Linux)
- CentOS 6.4 compiler installed MySQL 5.6.14 (Database)
- SUSE Linux firewall configuration notes (Linux)
- Windows 7 hard disk installation notes Debian (Linux)
- String JavaScript type system (Programming)
- Under Ubuntu 15.04 installation JDK8 (Linux)
- Firewall Configuration Red Hat Enterprise Linux 4 (Linux)
- Do not enter password login ssh (Server)
- Ubuntu 14.10 / 14.04 how to install Quick Start tool Mutate 2.2 (Linux)
- C ++ Const breaking rules (Programming)
- Configuring Android NDK development environment for Mac OS X (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.