如何在java中指定的时间延迟后启动一个线程 [英] How to start a thread after specified time delay in java

查看:2913
本文介绍了如何在java中指定的时间延迟后启动一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在ServletContextListener中调用了一个方法作为线程..现在根据我的需要我必须将线程延迟1分钟,然后开始执行线程中调用的方法,但我无法做到这一点,因为我非常这是我的代码...

  public class Startup实现ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent sce){
}

public void contextInitialized(ServletContextEvent sce) {
//你的创业公司在这里工作
System.out.println(Started ....);
// captureCDRProcess();
new Thread(new Runnable(){

@Override
public void run(){

captureCDRProcess();
}
})。start();

}

请帮助我..
提前致谢..

解决方案

要正确执行此操作,您需要使用ScheduledThreadPoolExecutor并使用函数日程安排,如下所示:

  final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(NUM_THREADS); 
executor.schedule(new Runnable(){
@Override
public void run(){
captureCDRProcess();
}
},1, TimeUnit.MINUTES);

Thread.sleep是不是的方式,因为它没有保证它在一分钟后醒来。根据操作系统和后台任务,它可能是60秒,62秒或3小时,而上面的调度程序实际上使用正确的操作系统实现进行调度,因此更加准确。



此外,此调度程序还允许其他几种灵活的方式来安排固定费率或固定延迟等任务。



编辑:使用新的Java8 Lamda语法的相同解决方案:

  final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(NUM_THREADS); 
executor.schedule(() - > captureCDRProcess(),1,TimeUnit.MINUTES);


I have called a method in ServletContextListener as thread ..Now as per my need i have to delay the thread for 1 minutes and then start executing the method called in the thread but i am not able to do that as i am very new in this...

Here is my code ...

public class Startup implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent sce) {
}

public void contextInitialized(ServletContextEvent sce) {
    // Do your startup work here
    System.out.println("Started....");
    //captureCDRProcess();
    new Thread(new Runnable() {

        @Override
        public void run() {

            captureCDRProcess();
        }
    }).start();

}

Please help me .. Thanks in advance..

解决方案

To do this properly, you need to use a ScheduledThreadPoolExecutor and use the function schedule like this:

final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(NUM_THREADS);
executor.schedule(new Runnable() {
  @Override
  public void run() {
    captureCDRProcess();
  }
}, 1, TimeUnit.MINUTES);

Thread.sleep is not the way to go, because it does not guarantee that it wakes up after a minute. Depending on the OS and the background tasks, it could be 60 seconds, 62 seconds or 3 hours, while the scheduler above actually uses the correct OS implementation for scheduling and is therefore much more accurate.

In addition this scheduler allows several other flexible ways to schedule tasks like at a fixed rate or fixed delay.

Edit: Same solution using the new Java8 Lamda syntax:

final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(NUM_THREADS);
executor.schedule(() -> captureCDRProcess(), 1, TimeUnit.MINUTES);

这篇关于如何在java中指定的时间延迟后启动一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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