解决方案 »

  1.   

    public class ShadowTest
    {
        
        public static void main(String[] args)
        {
            final long startTime = System.currentTimeMillis();
            
            final long endTime = startTime + 2000L;
            
            new Thread(()->
            {
                while (System.currentTimeMillis() < endTime)
                {
                    new Thread(new Read()).start();
                }
            }).start();
            
            try
            {
                Thread.currentThread().sleep(1000L);
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Config.getConfig().update();
            System.out.flush();
            System.out.println("update----------------------------------------------------------");
            Config.getConfig().update();
            System.out.flush();
            System.out.println("update----------------------------------------------------------");
            
            try
            {
                Thread.currentThread().sleep(2000L);
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            String str = Config.getConfig().getStr();
            System.out.println("Last str=" + str); 
        }
        
    }class Config
    {
        private static Config config = new Config();
        
        private static volatile int i = 0;
        
        private Config()
        {
            System.out.println("construct.....");
            System.out.println(i);
            str = Integer.toString(++i);
            System.out.println(i);
            System.out.flush();
        }
        
        private String str = null;
        
        public static Config getConfig()
        {
            return config;
        }
        
        public void update()
        {
            System.out.println("update...." + i);
            config = new Config(); 
            System.out.println("update~~~" + i);
            System.out.flush();
        }
        
        public String getStr()
        {
            return str;
        }
    }class Read implements Runnable
    {    @Override
        public void run()
        {
            String str = Config.getConfig().getStr();
            System.out.println("str=" + str);
            System.out.flush();
        }
        
    }
      

  2.   

    不管调用多少次,i的值都应该是update()次数加1啊
      

  3.   

     new Thread(()->    这是什么意思?
      

  4.   

    public class ShadowTest
    {
        
        public static void main(String[] args)
        {
            final long startTime = System.currentTimeMillis();
            
            final long endTime = startTime + 2000L;
            
            new Thread(){
                public void run() {
                    while (System.currentTimeMillis() < endTime)
                    {
                        new Thread(new Read()).start();
                    }
                };
                
            }.start();
            
            try
            {
                Thread.currentThread().sleep(1000L);
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Config.getConfig().update();
            System.out.flush();
            System.out.println("update----------------------------------------------------------");
            Config.getConfig().update();
            System.out.flush();
            System.out.println("update----------------------------------------------------------");
            
            try
            {
                Thread.currentThread().sleep(2000L);
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            String str = Config.getConfig().getStr();
            System.out.println("Last str=" + str); 
        }
        
    }class Config
    {
        private static Config config = new Config();
        
        private static volatile int i = 0;
        
        private Config()
        {
            System.out.println("construct.....");
            System.out.println(i);
            str = Integer.toString(++i);
            System.out.println(i);
            System.out.flush();
        }
        
        private String str = null;
        
        public static Config getConfig()
        {
            return config;
        }
        
        public void update()
        {
            System.out.println("update...." + i);
            config = new Config(); 
            System.out.println("update~~~" + i);
            System.out.flush();
        }
        
        public String getStr()
        {
            return str;
        }
    }class Read implements Runnable
    {    @Override
        public void run()
        {
            String str = Config.getConfig().getStr();
            System.out.println("str=" + str);
            System.out.flush();
        }
        
    }
    先还原了下你的代码;
    首先加载Config 时新建对象调用了一次构造函数,由于静态变量的初始化顺序是先config后i此时i=0
    后来两次调用update方法新建对应替换原上一次建的对象,又调用了两次构造方法,此时config对象的属性str="2"
      

  5.   

    那这一行呢:  new Thread(()->难道?你jdk版本是?