Interview FAQ: How to control multi-threaded high concurrent access in the bank online payment project?

Source: Internet
Author: User

Interview FAQ: How to control multi-threaded high concurrent access in the bank online payment project?

The Synchronized keyword mainly solves the problem of multi-threaded shared data synchronization.
Threadlocal mainly solves the problem that the data in multi-threading is inconsistent with concurrency.

Both threadlocal and synchonized are used to solve multi-threaded concurrent access. But there is an essential difference between threadlocal and synchronized:
Synchronized is the mechanism by which a lock is used so that a variable or block of code can be accessed by only one thread at a time. Instead, Threadlocal provides a copy of the variable for each thread, so that each thread accesses the same object at a certain time, isolating data sharing from multiple threads. Synchronized, in contrast, is used to gain data sharing when communicating between multiple threads.

Synchronized is used for data sharing between threads, while threadlocal is used for data isolation between threads. Of course threadlocal is not a substitute for synchronized, they deal with different problem domains. Synchronized is used to implement synchronization mechanisms and is more complex than threadlocal.

1. Synchronized usage in Java
The Synchronized keyword makes it easy to troubleshoot multi-threaded shared data synchronization issues.
The Synchronized keyword can be used as a modifier of a function, or as a statement within a function, that is, a synchronous method and a synchronous statement block. In the case of finer classifications, synchronized can be used for instance variables, object reference, static functions, and class literals (literal constants of classes).
The locks acquired by synchronized are objects, and each object has only one lock (lock) associated with it, and the implementation of synchronization is costly and may even cause deadlocks, so try to avoid unnecessary synchronization control.

4 ways to use synchronized:
1. When a method declaration is used, the thread obtains a member lock.
2. For a block of code, synchronized is followed by parentheses, in parentheses is the variable, and the thread obtains the member lock.
3.synchronized The following is an object in parentheses, at which point the thread obtains an object lock.
4.synchronized followed by a class in parentheses, at which point the thread obtains an object lock.

2. Usage of java.lang.ThreadLocal ()
I. Overview
What is ThreadLocal? In fact, ThreadLocal is not a local implementation version of a thread, it is not a threaded, but a threadlocalvariable (thread local variable). Perhaps it would be more appropriate to name it Threadlocalvar. The thread local variable (ThreadLocal) function is very simple, that is, for each thread that uses the variable to provide a copy of the value of a variable, is a special thread binding mechanism in Java, is that each thread can independently change its own copy, and does not conflict with the copy of other threads.

How does threadlocal maintain a copy of a variable for each thread? In fact, the idea of implementation is simple, there is a map in the Threadlocal class that stores a copy of each thread's variable.

To sum up, for the problem of multi-thread resource sharing, the synchronization mechanism adopts the way of "time-changing Space", and threadlocal adopts the way of "changing time by Space". The former provides only one copy of the variable, allowing different threads to queue access, and the latter provides a variable for each thread, so it can be accessed at the same time without affecting each other.

Second, API description

ThreadLocal ()
Creates a thread-local variable.

T get ()
Returns the value in the current thread copy of this thread's local variable, and if this is the first time a thread calls the method, creates and initializes the copy.

Protected T InitialValue ()
Returns the initial value of the current thread for this thread's local variables. This method is called at most once per access thread to get the local variable for each of the threads, that is, the first time the thread accesses the variable using the Get () method. If the thread calls the set (T) method before the Get method, then the InitialValue method is not called in the threads.
If the implementation returns only NULL, and if the programmer wants to initialize a thread-local variable to a value other than NULL, the subclass must be created for ThreadLocal and overridden by this method. Typically, anonymous inner classes are used. A typical implementation of InitialValue invokes an appropriate construction method and returns the newly constructed object.

void Remove ()
Removes the value of this thread's local variable. This may help reduce the storage requirements for thread-local variables. If you access this thread's local variable again, by default it will have its initialvalue.

void set (T value)
Sets the value in the current thread copy of this thread's local variable to the specified value. Many applications do not require this functionality, they rely only on the InitialValue () method to set the value of a thread-local variable.
The InitialValue method is generally overridden in a program to give a specific initial value.

Iii. Typical examples

Iv. Summary
Threadlocal mainly solves the problem that the data in multi-threading is inconsistent with concurrency.

Threadlocal provides a copy of the data that is accessed concurrently in each thread and runs the business through the access replica, which results in memory consumption, which greatly reduces the performance cost of thread synchronization and reduces the complexity of thread concurrency control.

Threadlocal cannot use atomic types, only object types. Threadlocal is much simpler to use than synchronized.

V. General steps for the use of threadlocal
1. In a multithreaded class (such as the Threaddemo Class), create a Threadlocal object threadxxx, which is used to save the object xxx between threads that need to be treated in isolation.
2. In the Threaddemo class, create a Method getxxx () that gets the data to be quarantined, and in the method it is judged that if the Threadlocal object is null, a new () object should be created to isolate the access type and cast to the type to be applied.
3, in the Threaddemo class of the Run () method, through the GetXXX () method to obtain the data to be manipulated, so that each thread can be guaranteed to correspond to a data object, at any time the operation of this object.

Interview FAQ: How to control multi-threaded high concurrent access in the bank online payment project?

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.