What is thread security?

Source: Internet
Author: User

Http://baike.baidu.com/view/1298606.htm

Thread Security

Directory

What is thread security?
Example
Thread Security
Thread Security Level
  1. Immutable
  2. Thread Security
  3. Conditional thread security
  4. Thread compatibility
  5. Thread opposition
What is thread security?
Example
Thread Security
Thread Security Level
  1. Immutable
  2. Thread Security
  3. Conditional thread security
  4. Thread compatibility
  5. Thread opposition
Expand

 

Edit this section. What is thread security?

If multiple threads are running simultaneously in the process where your code is located, these threads may run the code at the same time. If the result of each running is the same as that of a single thread, and the value of other variables is the same as expected, it is thread-safe.

 

In other words, the interface provided by a class or program is an atomic operation for a thread or the switching between multiple threads does not lead to ambiguity in the execution result of this interface, that is to say, we do not need to consider synchronization issues.

 

Thread security issues are caused by global variables and static variables.

 

If each thread only performs read operations on global variables and static variables without write operations, this global variable is generally thread-safe. If multiple threads execute write operations at the same time, generally, thread synchronization needs to be considered; otherwise, thread security may be affected.

Example of editing this section

For example, an arraylist class may take two steps to add an element: 1. Store this element at the items [size] position; 2. Increase the size value.

 

When a single thread is running, if the size is 0, after an element is added, the element is in the position 0 and the size is 1;

 

In the case of multithreading, for example, if there are two threads, thread a first stores the elements in the position 0. However, at this time, the CPU scheduling thread a is paused, and thread B gets the chance to run. Thread B also adds an element to this arraylist, because the size is still equal to 0 (note that we assume that two steps are required to add an element, thread a only completes step 1), so thread B also stores the elements in the position 0. Then, both thread a and thread B continue to run, increasing the size value.

 

Well, now let's take a look at the arraylist. There is actually only one element, which is stored in the position 0, but the size is equal to 2. This is "thread unsafe.

Edit thread security

To ensure thread security, you must first have the correct behavior in a single-threaded environment. If a class is implemented correctly (this is another way to say it complies with the specifications ), there is no operation sequence (read or write public fields and call public methods) for the objects of this class to make the objects invalid, it is observed that the object is in an invalid state, or violates any non-variable, pre-condition, or post-condition of the class.

 

In addition, a class should be thread-safe. When accessed by multiple threads, no matter what time sequence arrangement or staggered the running environment executes these threads, it must still have the correct behavior as described above, and there is no additional synchronization in the called code. The effect is that, in the view of all threads, operations on thread-safe objects occur in a fixed and globally consistent order.

 

The relationship between correctness and thread security is very similar to the relationship between consistency and independence used to describe acid (atomicity, consistency, independence, and durability) Transactions: from the perspective of a specific thread, object operations executed by different threads are sequential (although in an indefinite order) rather than parallel operations.

Edit thread security level

Thread security is not a true or false proposition. The vector method is synchronous, and the vector is clearly designed to work in a multi-threaded environment. However, its thread security is limited, that is, there is a State dependency between some methods (Similarly, if the vector is modified by other threads during iteration, it is determined by the vector. the iterator returned by iterator () will throw concurrentmodifiicationexception ).

 

No classification system is widely accepted for common thread security levels in Java classes. However, it is important to record their thread security behaviors when writing classes.

 

Bloch provides a classification method for describing five types of thread security: immutable, thread security, conditional thread security, thread compatibility, and thread opposition. As long as you clearly record the thread security features, it does not matter whether you use this system. This system has its limitations-the boundaries between various categories are not exactly clear, and in some cases it is not taken care of-but this system is a good starting point. The core of this classification system is whether the caller can or must be surrounded by external synchronization operations (or a series of operations ). The following sections describe the five categories of thread security.

Immutable

Immutable objects must be thread-safe and never require additional synchronization [1]. As long as an unchangeable object is built correctly, its external visible State will never change, and it will never be seen in an inconsistent state. In Java class libraries, most basic numeric classes such as integer, string, and biginteger are immutable.

Thread Security

The thread-safe object has the attributes described in the "thread security" section above -- the constraints specified by the class specification are still valid when the object is accessed by multiple threads, no additional synchronization is required for threads regardless of how the runtime environment is arranged. This thread security guarantee is very strict-many classes, such as hashtable or vector, cannot meet this strict definition.

Conditional thread security

Conditional thread security classes can be thread-safe for separate operations, but some operation sequences may require external synchronization. The most common example of conditional thread security is to traverse the fail-fast iterator returned by hashtable or vector or the returned iterator. It is assumed that the underlying set will not change during the iterator traversal.. To ensure that other threads do not change the set during traversal, the iterative thread should ensure that it exclusively accesses the set to achieve the integrity of traversal. In general, dedicated access is guaranteed by synchronization of locks-and class documents should indicate which lock (usually the internal monitor of the object (intrinsic Monitor )).

 

If you record a conditional thread security class, you should not only record it as conditional thread security, but also record the operation sequences that must be prevented from concurrent access. Users can reasonably assume that other operation sequences do not require any additional synchronization.

Thread compatibility

The thread compatibility class is not thread-safe, but can be safely used in the concurrent environment by correct use of synchronization. This may mean that each method call is surrounded by a synchronized block, or a wrapper object is created, where each method is synchronized (just like collections. synchronizedlist ). It may also mean that some operation sequences are surrounded by synchronized blocks. To maximize the use of the thread compatibility class, if all calls use the same block, the caller should not be required to synchronize the block. In this way, the thread-compatible objects will be included in other thread-safe objects as variable instances, so that synchronization of their owner objects can be used.

 

Many common classes are thread-compatible, such as the collection class arraylist and hashmap, java. Text. simpledateformat, or JDBC class connection and resultset.

Thread opposition

The Opposite Classes of threads are those that cannot be safely presented during concurrent use regardless of whether or not external synchronization is called. Thread opposition is rare. When the class modifies static data and static data affects the behavior of other classes executed in other threads, thread opposition usually occurs. An example of a thread opposition class is a class that calls system. setout.

References
  • 1

    Thread security level and category

    Http://www.ibm.com/developerworks/cn/java/j-jtp09263/

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.