How thread-safe is enum in java?

后端 未结 4 1860
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 08:01

How thread-safe is enum in java? I am implementing a Singleton using enum (as per Bloch\'s Effective Java), should I worry at all about thread safety for my singleton enum?

4条回答
  •  一整个雨季
    2020-11-29 08:53

    Adding synchronized avoids inconsistent state with enums.

    Code below will run will lock nicely alway printing "One". However when you comment out synchronized there will be other values printed too.

    import java.util.Random;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class TestEnum
    {
        public static AtomicInteger count = new AtomicInteger(1);
    
        public static enum E
        {
            One("One"),
            Two("Two");
    
            String s;
    
            E(final String s)
            {
                this.s = s;
            }
    
            public void set(final String s)
            {
                this.s = s;
            }
    
            public String get()
            {
                return this.s;
            }
        }
    
        public static void main(final String[] args)
        {
            doit().start();
            doit().start();
            doit().start();
        }
    
        static Thread doit()
        {
            return new Thread()
            {
                @Override
                public void run()
                {
                    String name = "MyThread_" + count.getAndIncrement();
    
                    System.out.println(name + " started");
    
                    try
                    {
                        int i = 100;
                        while (--i >= 0)
                        {
    
                            synchronized (E.One)
                            {
                                System.out.println(E.One.get());
                                E.One.set("A");
                                Thread.sleep(new Random().nextInt(100));
                                E.One.set("B");
                                Thread.sleep(new Random().nextInt(100));
                                E.One.set("C");
                                Thread.sleep(new Random().nextInt(100));
                                E.One.set("One");
                                System.out.println(E.One.get());
                            }
    
                        }
                    }
                    catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
                    System.out.println(name + " ended");
                }
            };
        }
    }
    

提交回复
热议问题