单例,无法进入可运行任务

Singleton, cannot go in the runnable task

我正在尝试 运行 自动执行一项任务(所有 30 多岁)。

为此,我构建了一个单例:

public class PortalSingleton {
    private static final Logger LOG = LoggerFactory.getLogger(PortalSingleton.class);
    private static final int INITIAL_DELAY = 0;
    private static final int DELAY = 30;

    private static volatile ScheduledExecutorService instance;
    private static HomepageView homeView = new HomepageView();

    private PortalSingleton() {}

    public static final void refreshGridHomePageAutomatically() {
        Runnable task = () -> UI.getCurrent().access(() -> {
            homeView.refreshGrid();
            LOG.info("The grid has been refreshed Automatically");
        });
        getInstance().scheduleWithFixedDelay(task, INITIAL_DELAY, DELAY, TimeUnit.SECONDS);
    }

    public final static ScheduledExecutorService getInstance() {
        if (instance == null) {
            synchronized (ScheduledExecutorService.class) {
                if (instance == null) {
                    instance = Executors.newScheduledThreadPool(1);
                }
            }
        }
        return instance;
    }
}

但是,我没有任何 issue/error 而且我没有我的日志消息,我的网格也没有被刷新..

预期的行为是:

  1. 我的网格刷新
  2. 查看日志消息

即使我删除行 homeView.refreshGrid();,我也没有我的日志信息...

我做错了什么?

谢谢,

编辑:我这样称呼它:PortalSingleton.refreshGridHomePageAutomatically();

EDIT2,感谢@Holger:

public class PortalSingleton {
    private static final Logger LOG = LoggerFactory.getLogger(PortalSingleton.class);
    private static final int INITIAL_DELAY = 0;
    private static final int DELAY = 30;

    private static final ScheduledExecutorService instance = Executors.newScheduledThreadPool(1);
    private static HomepageView homeView = new HomepageView();

    private PortalSingleton() {
    }

    public static final void refreshGridHomePageAutomatically() {
        Runnable task = () -> UI.getCurrent().access(() -> {
            homeView.refreshGrid();
            LOG.info("The grid has been refreshed Automatically");
        });
        try {
            getInstance().scheduleWithFixedDelay(task, INITIAL_DELAY, DELAY, TimeUnit.SECONDS);
        } catch (Exception e) {
            LOG.error("error" + e);
        }
    }

    public final static ScheduledExecutorService getInstance() {
        return instance;
    }
}

安排操作时,发生异常时不会收到反馈。相反,它只会停止执行它:

ScheduledExecutorService.scheduleWithFixedDelay(…):

…If any execution of the task encounters an exception, subsequent executions are suppressed.

因此,您必须在操作本身中使用 try … catch 块来报告它,例如在定义 Runnable:

的 lambda 表达式中
Runnable task = () -> {
    try { UI.getCurrent().access(…); }
    catch (Exception e) { LOG.error("error" + e); }
};

我觉得你从非 UI 线程调用 UI.getCurrent() 看起来很可疑,我怀疑 return null 导致了 NullPointerException尝试对其调用方法时。