Thread Safety Summary

Source: Internet
Author: User
Thread Safety Summary 1. What is a thread safety issue? When the server receives multiple accesses at the same time, the server assigns a thread to each request. And if these threads call the same method, and the method has a global variable, and this method also writes, the value of the variable is changed. There is a thread-safety issue at this point. The request will not be the same as the desired result, or even a system run-time error will occur. For example, a collection is looped and changes his value, and at this point the value of a run-time exception may occur because a thread also iterates. 2. Workaround. There are two first methods: Thread lock. The thread lock is that when multiple threads access a method at the same time, it is judged that the other line threads has not finished this method. If other threads are not finished, these threads can only exclude waiting. The other thread may run only when one thread is finished. A second approach. Threadlocal local thread. Add the instance object to the threadlocal and use it to get the 3.spring thread security problem in the threadlocal. In Springmvc, when creating controler, the default is singleton, because Springmvc is a method-based presentation layer framework that adds variables directly to a method's member variables when a parameter is requested, so there is no thread-safety problem if you do not define a member variable. Of course, a thread-safety issue occurs when you define an instance object (member variable) and write. Spring is a singleton when creating a bean object, and there is a thread-safety issue. should be noted. 4.strus thread safety issues when using struts, it is generally used with spring to have spring create the action object for struts, and strusts will use member variables when receiving the request parameters. All if you create a single instance of action, there is a thread-safety issue. All the time in struts is usually a lot of examples. Each request creates an action.

Thread Safety Summary

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.