What security issues can occur with threads

Source: Internet
Author: User

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, looking forward to working with you
Communication! ------


What security issues can occur with threads
Singleton design pattern: To ensure the uniqueness of the object
Class single
{
private static single s=null;
Private single () {}
public static single Getsingle ()
{
if (s==null)
return new single ();
}

}
We all know that the singleton design pattern is designed to guarantee the uniqueness of the object, but the problem may occur when the pattern is multithreaded.
Because when a thread executes to an if (s==null) statement, the CPU execution may be snatched by another thread, so that the thread has already judged the s==null.
Once again, after he had taken the executive power, he would no longer judge S==null, but was directly new to the object.

So imagine: The thread is waiting to be executed again, another process may come in. Then the second thread executes during execution
If it is not snatched, the second thread is not judged by the s==null to true, thereby the new object. So that when the first thread executes again,
Times New object, then the object is not unique. Violates the principle of single-instance design pattern

Java provides a professional solution to multi-threaded security issues: Synchronization

Principle: For multiple operations to share data statements, only one thread can be executed, the execution of other threads can not participate in execution, that is, lock!

Prerequisites for synchronization:
1, must have more than two threads
2, must be multiple threads using the same lock

Synchronous code block: The lock used is an object
Synchronized (object)
{
Code blocks that need to be synchronized
}

Sync function: The lock used is this

Just put the Synchronized keyword on a function that needs to be synchronized
Example: public synchroized void ...

static function: The lock used is the class name of the bytecode file object of the class in which the function resides. class

How to find the code that needs to be synchronized:
Which code needs to be synchronized through three explicit to find:
1, clear which code is multithreaded run code
2, clear sharing of data
3, clear which statements in multithreaded run code are operations that share data

In accordance with the above principles to the single case design plus synchronization
Class single
{
private static single s=null;
Private single () {}
public static single Getsingle ()
{
if (s==null)
Synchronized (Single.class)
{
if (s==null)
return new single ();
}
}

}

This ensures that the thread is secure and that the object is still unique.
Advantages and disadvantages of synchronization
Benefits: resolves multi-threaded security issues
Cons: Multiple threads each time to determine the lock, consume resources, plus double judgment can slightly improve efficiency.


------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, looking forward to working with you
Communication! ------

What security issues can occur with threads

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.