我有这个方法:
public class NFeRoboServiceImpl implements NFeRoboService {
private static volatile ThreadPoolExecutor executor;
private ThreadPoolExecutor getExecutor() {
if (executor == null) {
synchronized (NFeRoboServiceImpl.class) {
if (executor == null) {
executor = new ThreadPoolExecutor(2, 10, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100, true), new RetryRejectedExecutionHandler());
Monitor monitor = new Monitor(nFePersistenceService, executor, 3);
Thread monitorThread = new Thread(monitor);
monitorThread.start();
}
}
}
return executor;
}
}
问题很奇怪,当我把光标放在新的ThreadPoolExecutor之后的'executor'之上时,它说对象是null,当我选择执行器并且 ctrl + shift + i ,它显示了我创建的对象。
我在线程执行器上有空:
getExecutor().execute(worker);
答案 0 :(得分:0)
我认为,执行者是静态的。在这种情况下,您应该将其定义为volatile。 请参阅&#34;仔细检查&#34;回答Should I synchronize a static volatile variable?
见:
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
由于Bean-Convention,当eclipse试图确定执行者的值时,可能会使用getExecutor。如果将隐式初始化函数重命名为与fieldname无关的内容会发生什么?