Why is this class not thread safe?

前端 未结 7 1321
误落风尘
误落风尘 2021-01-30 09:52
class ThreadSafeClass extends Thread
{
     private static int count = 0;

     public synchronized static void increment()
     {
         count++;
     }

     public          


        
7条回答
  •  庸人自扰
    2021-01-30 10:26

    Since two different methods, one is instance level and other is class level, so you need to lock on 2 different objects to make it ThreadSafe

提交回复
热议问题