Coverity 0 Bad choice of lock object--set do lock

Source: Internet
Author: User

When we try to synchronized a collection, coverity scans with a hint of bad choice of lock object.

Refer to the following code:

public class Test {public static void main (string[] args) throws Exception {integer in = new Integer (123
        29); Thread1 thread1 = new Thread1 (in);
        
        If the lock object is a map above, you can modify the success Thread2 thread2 = new Thread2 (in);
        New Thread (Thread1). Start ();
    New Thread (Thread2). Start ();

    } class Thread1 implements Runnable {Integer in;
    Public Thread1 (Integer in) {this.in = in;
                @Override public void Run () {synchronized () {try {
                System.out.println ("Thread1 start running ...");
                In.wait (6000l);  in = 32;
            This will be an error java.lang.IllegalMonitorStateException System.out.println (in);
            catch (Interruptedexception e) {e.printstacktrace ();

    Class Thread2 implements Runnable {Integer in;
   Public Thread2 (Integer in) {     This.in = in;
        @Override public void Run () {in = 10;
    System.out.println ("Put finish ...");
 }
}

The reason to use an integer object to make an error is to lock the integer object, but try to modify the object.

When using a collection object, such as a map, in our synchronized, refer to the following code:

public class Test {public static void main (string[] args) throws Exception {map<string, string> m
        AP = new hashmap<string, string> ();
        Map.put ("1000", "6");
        Thread1 thread1 = new Thread1 (map);
        
        Thread2 thread2 = new Thread2 (map);
        New Thread (Thread1). Start ();
    New Thread (Thread2). Start ();

    } class Thread1 implements Runnable {map<string, string> Map;
    Public Thread1 (map<string, string> map) {this.map = map;
                @Override public void Run () {synchronized (map) {try {
                System.out.println ("Thread1 start running ...");
                Map.wait (6000l); For (map.entry<string,string> Entry:map.entrySet ()) {System.out.println (Entry).
                Getkey () + ":" + entry.getvalue ());
            catch (Interruptedexception e){E.printstacktrace ();

    Class Thread2 implements Runnable {map<string, string> Map;
    Public Thread2 (map<string, string> map) {this.map = map; @Override public void Run () {for (int i=0 i < 5; i + +) {String key = Str
            Ing.valueof (i);
            String value = string.valueof (i);
        Map.put (key, value);
    } System.out.println ("Put finish ..."); }

}

When you use the map, you can see that although the map is synchronized, another thread can modify the map. Why, then?

When we use a collection, synchronized locks the address of the collection, as if the final modifier set or array, the reference is not modifiable, but the contents of the collection can be modified. Coverity Scan the code, it will try to avoid this situation, do not let us use this mode of locking.

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.