这段代码Last str是2,谁能告诉我为什么? staticjava 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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(); } } 不管调用多少次,i的值都应该是update()次数加1啊 new Thread(()-> 这是什么意思? 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" 那这一行呢: new Thread(()->难道?你jdk版本是? java 随机数问题 jdbc更新停顿问题。orcale9i,使用ojdbc14.jar 替换{$h1}问题 JFileChooser 里面的撤销按钮怎样加点击事件? 怎样才能让动画GIF在应用程序的一个界面上显示啊? 打开网页超时问题 APPLET运行出错,求助 static member variable的初始化问题 新手的问题???? 同学找我做一个小项目,我报多少钱合适 java 静态和非静态的一点疑问 dos 界面 为什么没有任何输出 也没有任何错误提示 求大神指点!!!
{
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();
}
}
{
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"