Potential concurrency issue when an Integer object is used as a lock in a synchronized block

While working on a multi-threaded program, I observed very strange behavior.

When an Integer object is used as a lock, it seems that several threads may be in a synchronized block. Even the thought that is not expected.

If I use any other static member like 's', 'o' and 'c' in the program below, it works as expected.

the code -

public class MyThread extends Thread{
    private static Integer ii=1;                        //Works fine
    private static Integer i=1;
    private static String s="1";                        //Works fine
    private static Object o= new Object();              //Works fine
    private static Class<MyThread> c= MyThread.class;   //Works fine

    public void run(){
        synchronized(i){
            System.out.print(i++ +" ");
            System.out.print(i+"   ");
        }
    }
    public static void main(String[] str) throws InterruptedException{
        for(int i=0;i<100;i++){
            MyThread t= new MyThread();
            t.start();
        }
        Thread.sleep(100);
        System.out.println();
        MyThread t= new MyThread();
        t.start();t.join();
        if(i!=102)
            System.out.println("fail");
    }
}

output -

2 3   3 4   5 6   8 9   9 10   10 11 12   12 13   13 14   14 15   16 17   1 17   15 17   12   17 18   18 20   20 21   21 22   7 22 6 23   4 23   23 24   24 25   25 26   23   22   19 27   26 27   27 28   28 29   29 30   30 31   31 32   32 33   33 34   34 35   35 36   36 37   37 38   38 39   39 41   40 41 42   42   42 43   43 44 45   45   45 48   47 48   46 48   48 49   49 50   50 51   51 52   52 53   53 54   54 55   55 56   56 57   57 58   58 59   59 60   60 61   61 62   62 63 64   64 65   64   65 66   66 67   68 69   69 70   67 70   70 71   71 72   72 73   73 75   74 76   75 76   76 77   77 79 80   78 80   80   80 83 84   82 85   85 86   86 87   87 88   88 89   89 90   81 94   93 94   92 94   91 94   90 94   84   94 96   96 98   98 99   84 99   97 99   95 99   99 100   100 101   
101 102  

As you can see, when he printed “10 11 12”, two threads were running in the synchronized block.

Am I doing something wrong or am I missing something?

Does this need to be done with some optimization behind the scenes? Because if I used “ii” to lock, everything works fine.

, "i", "fail", RARELY.

Java, . java -version java version "1.7.0_51" Java (TM) SE Runtime Environment (build 1.7.0_51-b13) Java HotSpot (TM) ( 24.51-b03, , )

.

public class MyThread extends Thread{
    private static Integer ii=1;
    private static Integer i=1;
    private static String s="1";
    private static Object o= new Object();
    private static Class<MyThread> c= MyThread.class;

    public void run(){
        synchronized(ii){
            System.out.print(i++ +" ");
            System.out.print(i+"   ");
        }
    }
    public static void main(String[] str) throws InterruptedException{
        for(int j=0;j<100;j++){
            for(int i=0;i<100;i++){
                MyThread t= new MyThread();
                t.start();
            }
            Thread.sleep(50);
            System.out.println();
            MyThread t= new MyThread();
            t.start();t.join();
            if(i!=102)
                System.out.println("fail");
            Thread.sleep(50);
            i=1;
            System.out.println();
        }
    }
}
+4
4

i++

i = Integer.valueOf(i.intValue() + 1)

, i , , .

synchronized i, , .

, "ii" , .

ii .

+7

final. , .

+1

, , "i", i++ , , , i, ( Eclipse, ).

0

Integer- immutable class. When you perform a type operation ++, you create a new instance. This can lead to synchronization of threads on different objects, which allows them to simultaneously access the block synchronized.

0
source

Source: https://habr.com/ru/post/1529014/


All Articles