ScheduledExecutorService,并将日期格式添加到ActionListener [英] ScheduledExecutorService with dating format added to ActionListener

查看:67
本文介绍了ScheduledExecutorService,并将日期格式添加到ActionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解在Java中,可以使用 ScheduledExecutorService 在特定延迟后执行特定任务.帖子显示了如何执行在特定日期执行任务,但不使用 SimpleDateFormat .例如,我的格式初始化如下:

I understand that in Java, it is possible to use a ScheduledExecutorService to perform a specific task after a certain delay. This post shows how to execute the task at a specific date, but not with a SimpleDateFormat . For example, I have a format initialized like below:

dateFormatter = new SimpleDateFormat("MM-dd-yyyy hh:mm aa");

此外,我想执行 ActionListener 中的 else if 语句下声明的特定任务,例如:

Additionally, I want to execute a particular task declared under an else if statement in an ActionListener , such as below:

foo.addActionListener(e -> {
            if (condition) {

            // some task

            } else if (some other condition) { // what I want to be executed at particular date

            // some other task

            } else {

            // another task

            }
        });

我如何初始化在特定日期和/或在其他情况下使用 else if 语句执行的 ScheduledExecutorService ,最好使用 SimpleDateFormat ?

How could I initialize a ScheduledExecutorService that is executed within and/or with an else if statement at a certain date, preferably with a SimpleDateFormat ?

推荐答案

更早地建立执行程序服务

如何初始化 ScheduledExecutorService ,在

您没有.

请勿在需要时初始化执行程序服务.

Do not initialize your executor service at the point where you need it.

您实例化

You instantiate your scheduled executor service elsewhere, earlier, keeping a reference in a named variable. Call upon that executor service object as needed, later.

可以调用该执行程序服务来在应用程序的其他部分中运行其他类型的任务.执行程序服务不必仅绑定到一种任务.

That executor service can be called on to run other kinds of tasks in other parts of your app. The executor service need not be tied to only a single kind of task.

重要,您必须保留对该执行程序服务的引用,以便可以在某个时候正常关闭其后台线程池.否则,线程池可能会在其原始应用程序结束后继续运行.

Important You must keep a reference to that executor service so that it’s backing thread pool can be gracefully shut down at some point. Otherwise the thread pool may continue running after its originating app has ended.

通常,我建议采用这种方法.

Generally, I suggest this approach.

  • When the app launches, establish your executor service. Keep a reference to be accessed later via your choice of a global variable. Perhaps a Singleton, or a Service Locator, or dependency injection such as passing to a constructor.
  • During your app’s run, locate that existing executor service. Submit your task to be run.
  • When the app is ending, locate that existing executor service. Call its shut-down method to end its backing thread pool.

搜索堆栈溢出.这个话题已经被讨论过很多次了.您将找到示例代码和进一步的讨论.

Search Stack Overflow. This topic has been covered many times. You will find example code and further discussion.

使用

Use the Executors utility class to instantiate an executor service.

ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor() ; 

将该 ses 参考存储在您的应用中的某个位置.

Store that ses reference somewhere in your app.

计算等待时间,如 Ole V.V.的正确答案

foo.addActionListener(
    e -> {
        if (condition) {

            doSomethingNow() ;

        } else if (some other condition) { 

            long delaySeconds = … ;  // See: https://stackoverflow.com/a/60963540/642706
            ScheduledExecutorService scheduledExecutorService = Objects.requireNonNull​( … ) ;  // Locate the existing scheduled executor service.
            Runnable task = () -> { System.out.println( "Doing something later. " + Instant.now() ) ; };
            scheduledExecutorService.schedule(
                task ,
                delaySeconds ,
                TimeUnit.SECONDS
            );

        } else {

            doSomethingElseNow() ;

        }
    }
);

退出应用程序时,请关闭该计划的执行程序服务.

When your app is quitting, shut down that scheduled executor service.

// In the hook for app shut-down.
ScheduledExecutorService scheduledExecutorService = Objects.requireNonNull​( … ) ;  // Locate the existing scheduled executor service.
scheduledExecutorService.shutdown() ;

这篇关于ScheduledExecutorService,并将日期格式添加到ActionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆