public class Test { public static boolean flag=false;
private static Runnable run=new Runnable() {
@Override
public void run() {
while (true){
// System.out.println(); ①
if (flag){
System.out.println("this is sub thread..........................");
}
}
}
};
public static void main(String[] args) throws InterruptedException {
new Thread(run).start();
Thread.sleep(1000);
flag=true;
}
}
如题,子线程里的代码是不可能执行的,但是把①处的注释放开,子线程里的代码就可以执行。请问各路大神,是什么原因呢?
private static Runnable run=new Runnable() {
@Override
public void run() {
while (true){
// System.out.println(); ①
if (flag){
System.out.println("this is sub thread..........................");
}
}
}
};
public static void main(String[] args) throws InterruptedException {
new Thread(run).start();
Thread.sleep(1000);
flag=true;
}
}
如题,子线程里的代码是不可能执行的,但是把①处的注释放开,子线程里的代码就可以执行。请问各路大神,是什么原因呢?
解决方案 »
- hibernate无法级联删除,大家来帮我看下啊!
- window7 家庭基础版 能够配置java ee 网络开发的环境吗?
- el表达式问题
- Not binding factory to JNDI, no JNDI name configured
- struts2 Bean取值问题,在线等,谢谢。
- 在Struts的DTO里,为什么要用构造函数复制
- 程序自动启动的问题??在线等,急
- RMI的问提,怎么问了这么久,可还是没人知道!!!
- 100分求问,jb中用junit测一个通过DataSource得到数据库连接的类,出现的问题
- 如何用Java设计一个小程序,界面显示图点击图片后可播放音乐
- 路径不出错的情况下一直是servlet 404错误 该怎么办?
- could not resolve property: store_id of: hezeu.ord.j2ee.pojo.Remark [from hezeu.
public static boolean flag=false;
private static Runnable run=new Runnable() {
@Override
public void run() {
while (true){
System.out.println("***********");
if (flag){
System.out.println("this is sub thread.........................."+new java.sql.Timestamp(System.currentTimeMillis()).toString());
}
}
}
};
public static void main(String[] args) throws InterruptedException {
new Thread(run).start();
Thread.sleep(1000);
flag=true;
}
}
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:44:54.313
***********
this is sub thread..........................2018-07-02 08:4