Java concurrency-Adding an atomic method to an existing thread-safe class

Source: Internet
Author: User
Tags visibility

Java has a lot of thread-safe basic module classes, and in general, these basic module classes can meet all of the operations we need, but more often than not, they do not meet all of our needs. At this point, we need to find a way to add a new atomic operation without destroying the existing thread-safe class. The following 4 scenarios are available:
1 Modify the source code of the class to add a new atomic operation
2 Inherit the thread-safe class and add atomic operations
3 Using the Client lock method
4 Use combination method (recommended)
Generally speaking, the way to modify the source code is not very feasible, this will destroy the original class packaging and sometimes, the source is not available. Let's start with an example in the second way:

Suppose now for a class vector, we know that it is a thread-safe class. If you want to add a "if not add" method to him, proceed as follows:

Java code
    1. public class  Improvedvector<t> EXTENDS&NBSP;VECTOR<T>{&NBSP;&NBSP;
    2.     public synchronized  boolean putifabsent (t x) {  
    3.         boolean flag=contains (x);   
    4.         if (!flag)    
    5.              add (x);   
    6.         return !flag;   
    7.     }  
    8. }  


Let us analyze the above code: using the built-in lock of the Improvedvector class object guarantees the Atomicity of the contains () and add () methods, because the built-in lock of the Improvedvector class object is the built-in lock of the vector class object (that is, the Add () Method and The contains () method, which guarantees the visibility of the Add () method and The contains () method to achieve the desired effect.

An example of the third method:

Java code
  1. (The wrong implementation)
  2. Public class improvedlist<t>{
  3. Public list<t> list=collections.synchronizedlist (new arraylist<t> ());
  4. Public Synchronized boolean putifabsent (T x) {
  5. boolean flag=list.contains (x);
  6. if (!flag)
  7. List.add (x);
  8. return!flag;
  9. }
  10. }


The above example is an example of error, which we will analyze: first, synchronized guarantees the atomicity of the List.contains () method and the List.add () method, assuming there is now a class object in the execution of the Putifabsent () method, and is going to execute (not yet) List.add (2) method, at this time, another thread preemptive execution of the List.add (2) method, after the completion of the thread, the lock of the list is released, and then will be executed (not yet executed) the List.add (2) method begins to get the CPU and execute. Look, in this process, the number 2 is added 2 times. That is, the above code only guarantees the atomicity of the contains () method and the Add () method, as well as the mutex of the list reference operation, and does not guarantee the visibility of the List.add () method.
Think about it, the problem is that the lock of the Putifabsent () method is not the same as the lock of the list object, and the lock of the Putifabsent () method is the lock of the Improvedlist class, and List.add () The lock of the method is the lock used by the collections.synchronizedlist (), so the code above is changed to:

Java code
  1. (The correct implementation)
  2. Public class improvedlist<t>{
  3. Public list<t> list=collections.synchronizedlist (new arraylist<t> ());
  4. Public Synchronized boolean putifabsent (T x) {
  5. synchronized (list) {
  6. boolean flag=list.contains (x);
  7. if (!flag)
  8. List.add (x);
  9. return!flag;
  10. }
  11. }
  12. }



An example of the fourth method:

Java code
  1. Public class Improvedlist<t> implements list<t>{
  2. Private final list<t> List;
  3. Public improvedlist (list<t> List) {
  4. this.list=list;
  5. }
  6. Public Synchronized boolean putifabsent (T x) {
  7. boolean flag=list.contains (x);
  8. if (!flag)
  9. List.add (x);
  10. return!flag;
  11. }
  12. }
  13. ... Implementing other methods in the List<t> interface
  14. }


At first glance found that the above code in terms of security seems a lot weaker, putifabsent (T x) method of the FIANL variable list may be the thread security class is not, but for the above code, We have a hypothesis (when a list object is passed to Improvedlist's constructor, the client code will no longer use the object, but instead use its corresponding Improvedlist object), with this assumption, the code above is thread-safe.

Above is the 4 methods of adding a synchronization function on top of an existing thread-safe class in Java concurrency Programming.

Java concurrency-Adding an atomic method to an existing thread-safe class

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.