Case and analysis
Problem background
In Tomcat, the following code is inside the WebApp, causing a WebappClassLoader leak that cannot be reclaimed.
public class MyCounter {private int count = 0;
public void Increment () {count++;
public int GetCount () {return count; } public class Mythreadlocal extends threadlocal
The above code, as long as LeakingServlet it is invoked once, and the thread that executes it does not stop, it can
I. Introduction of Threadlocalthreadlocal is a local variable of a thread that is held separately by each thread, which other threads cannot access , usually a private static field in a class.We know that sometimes a variable of an object is accessed by multiple threads, then there is a thread security problem, and of course we can use the Synchorinized keyword to lock the variable and synchronize it, thus restricting only one thread to use this variable, but locking can greatly affect program e
(int i = 0; i Output:Thread-0 1Thread-0 2Thread-0 3Thread-1 1Thread-2 1Thread-2 2Thread-2 3Thread-1 2Thread-1 3From the above output you can see that each thread is reading the value of the copy above the thread itself, and it modifies these without involving the original object.Summary: This section briefly describes the synchronization and threadlocal of synchronized on other objects to eliminate the synchronization of shared objects.This chapter i
Let's talk about the implementation of Threadlocal and its memory leak problemLet's start with an official example, where a ThreadID class is constructed to hold the respective ID in each thread, which is globally unique and can be obtained by getting the ID.1 Private Static classThreadId {2 //Atomic integer containing the next thread ID to be assigned3 Private Static FinalAtomicinteger NextID =NewAtomicinteger (1);4 //thre
Threadlocal's core idea is simple: provide a copy of a variable for each individual thread.
The Java-provided synchronized keyword uses a "sync lock" mechanism to block threads from competing access, which means "space for time". : "10PT; Font-size:> threadlocal Use the "copy copy" way, everyone has a share, you use your, I use my, we do not affect each other, is "space for time." When each thread modifies
In order to make up the word, the Oracle document introduced threadlocal copied overThreadlocalThis class provides thread-local variables. These variables differ from their normal counterparts in so each thread this accesses one (via itsGetOrSetmethod) has its own, independently initialized copy of the variable.ThreadLocalInstances is typically private static fields in classes this wish to associate state with a thread (e.g., a user ID or Tr Ansaction
5. Other
5.1, ThreadLocal
A solution to the problem of member variables in a multithreaded environment, but it has nothing to do with thread synchronization. The idea is to create a separate copy of the variable for each thread, so that each thread can independently change its own copy of the variable without affecting the corresponding copy of the other thread.
Threadlocal is not intended to solve the pr
1. Brief introduction of ThreadlocalThreadlocal instances are typically present in a class as static private static fields that are used to associate a thread. ThreadLocal is a thread-level local variable, and the following is a key point for thread-local variables (ThreadLocal variables):A, when using threadlocal to maintain variables, if multiple threads access
variables in this method are copied one minute to the line stacks. So local variables are not shared by multiple threads, and there is no concurrency problem. So we can use local variables to not use global variables, global variables easily cause concurrency problems.3: A more canonical way to maintain thread closeness is to use the ThreadLocal class, which associates a value in a thread with the object that holds the value. The
Impersonation threadlocal class implementation: Thread-scoped shared variables, each thread can only access his or her own, and cannot access other threads.
Package com.ljq.test.thread;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.Random; /** * thread-wide Shared variables * * Three modules shared data, main thread module and AB module * * @author Administrator */public class Threadscopesharedata {//
Ready to share data pr
first, the introduction of Threadlocal/* Test ThreadLocal Object * ThreadLocal: This class provides a thread-local variable. * These variables are independent in the case of shared access in different line thread (must be accessed through the get and set methods). * It is obvious that this class provides a mechanism to prevent the unsafe mechanism of multi-threa
Threadlocal is used for the following scenarios:1. Resources that are not allowed to be accessed concurrently by multiple threads2. Single thread survival process uses only one instanceThe official definition is as follows:Private static fields in classes this wish to associate state with a thread (e.g., a user ID or Transaction ID).Examples of Use (official example: each thread has its own individual ID, and this ID remains self-increasing as new thr
(!exit);
}
public static void Main (string[] args) throws Exception
{
Threadflag thread = new Threadflag ();
Thread.Start ();
Sleep (5000); Main Thread Delay 5 sec
thread.exit = true; Terminate Thread Threads
thread.join ();
SYSTEM.OUT.PRINTLN ("Thread exit!");
}
An exit flag exit is defined in the code above, and when Exit is true, the while loop exits, and exit defaults to False. When you define exit, you use a
content: ThreadLocal: allows us to create variables that can only be read and written by the same thread, such as a web app that moves a variable from the front to the background, and needs to always be available at any time in the thread of the request. The internal implementation is implemented through a THREADLOCALMAP map structure that takes the thread object as a key and a variable copy as value. public class Testthreadlocal {public static class
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.