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();