自建线程中的一段代码不进入,有输出语句或debug击断点就能进入,求大神分析下,拜谢!
自建线程中的一段代码不进入,有输出语句或debug打断点就能进入,求大神分析下,拜谢!!!!
解释一下:这是个监听器,项目启动时开启一个线程。Constant.ctx这个变量在项目启动时是空的, while (notIOC) {...}是等待ctx被赋值,一旦被赋值notIOC 为false,代码继续往下走。
问题:项目启动后到了while (notIOC) {...}这部分就不往下走(此时已经确定Constant.ctx不为空了) ,“出来了”这几个字打印不出来,什么错也不报;但如果在第28行加入打印语句 就会正常执行,或者在代码里加个断点也会马上进入,然后正常执行。
折磨我好几天的问题,只好求助****大神了。。。
------解决思路----------------------
public static ApplicationContext ctx = null;
前面加个violiate试试
@Component
public class TaskListener {
private static final Logger logger = LoggerFactory.getLogger(TaskListener.class);
private static boolean notIOC = true;
@Value("#{propertiesReader.listenerInterval}")
private int listenerInterval;
private ExecutorService executorService = Executors.newFixedThreadPool(1);
@PostConstruct
public void initProducer() {
executorService.submit(new GetTaskSend());
}
@PreDestroy
public void destoryProducer() {
executorService.shutdown();
}
class GetTaskSend implements Runnable {
public void run() {
while (true) {
System.out.println("进来了");
while (notIOC) {
// logger.info("Waiting for Spring IOC...");
if (Constant.ctx != null) {
notIOC = false;
}
}
System.out.println("出来了");
//业务代码略...
try {
Thread.sleep(listenerInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
解释一下:这是个监听器,项目启动时开启一个线程。Constant.ctx这个变量在项目启动时是空的, while (notIOC) {...}是等待ctx被赋值,一旦被赋值notIOC 为false,代码继续往下走。
问题:项目启动后到了while (notIOC) {...}这部分就不往下走(此时已经确定Constant.ctx不为空了) ,“出来了”这几个字打印不出来,什么错也不报;但如果在第28行加入打印语句 就会正常执行,或者在代码里加个断点也会马上进入,然后正常执行。
折磨我好几天的问题,只好求助****大神了。。。
------解决思路----------------------
public static ApplicationContext ctx = null;
前面加个violiate试试