2016-11-24 76 views
0

我想要一个任务在延迟3秒后执行,而我的一个任务需要2秒才能完成。在执行器服务延迟后安排任务

我得到是显示5秒

注意间隔输出:学生类实现Callable接口 我有以下疑问

  1. 为什么有5秒的延迟如何延迟3秒 秒为什么线程1显示在第二次执行中,应该是 线程2

我得到的输出是

The time is : Sat Nov 26 15:08:02 IST 2016 

Doing a task during : prerna - Time - Sat Nov 26 15:08:06 IST 2016 
pool-1-thread-1 Helloprerna 
Doing a task during : abc - Time - Sat Nov 26 15:08:11 IST 2016 
pool-1-thread-1 Helloabc 
Doing a task during : def - Time - Sat Nov 26 15:08:16 IST 2016 
pool-1-thread-2 Hellodef 
Doing a task during : xyz - Time - Sat Nov 26 15:08:21 IST 2016 
pool-1-thread-1 Helloxyz 
Doing a task during : ritu - Time - Sat Nov 26 15:08:26 IST 2016 
pool-1-thread-3 Helloritu 
Doing a task during : babita - Time - Sat Nov 26 15:08:31 IST 2016 
pool-1-thread-2 Hellobabita 

代码:

private String display(String name2) { 

    try { 
     // System.out.println(Thread.currentThread().getName()); 
     name2=Thread.currentThread().getName()+" Hello"+ name; 
     System.out.println("Doing a task during : " + name + " - Time - " + new Date()); 
     Thread.sleep(000); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return name2; 
} 


@Override 
public String call() throws Exception { 
    // TODO Auto-generated method stub 
    if (name == "archana") { 

     throw new Exception(); 
    } 
     /*} catch (Exception e) { 
      // TODO Auto-generated catch block 
     // e.printStackTrace(); 
     }finally{ 
      return "error"; 
     }*/ 

    return display(name); 
} 

public class ExecutorScheduleDemo { 

    public static void main(String args[]) throws InterruptedException{ 
     ScheduledExecutorService executor= Executors.newScheduledThreadPool(5); 
     ArrayList<Student> list = new ArrayList<Student>(); 

     list.add(new Student("prerna")); 
     list.add(new Student("abc")); 
     //list.add(new Student("archana")); 
     list.add(new Student("def")); 
     list.add(new Student("xyz")); 
     list.add(new Student("ritu")); 
     list.add(new Student("babita")); 
     System.out.println("The time is : " + new Date()); 
     List<Future<String>> resultList= new ArrayList<Future<String>>(); 
     for(Student s:list){ 
      Future<String> f=executor.schedule(s, 3, TimeUnit.SECONDS); 

      try { 
       System.out.println(f.get()); 
      } catch (ExecutionException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

究竟是什么问题?它不清楚 –

回答

0

要完成eeedev的答案,因为你的对象似乎是一个Callable

您只需通过你的Callable的构造函数创建一个新FutureTask,如oracle docs

注意描述FutureTask的类型参数必须与Callable's相同。

例子:

class Main { 

    public static void main(String[] args) { 
     Foo foo = new Foo(); 
     FutureTask<String> fooFutureTask = new FutureTask<>(foo); 
    } 
} 

class Foo implements Callable<String> { 

    @Override 
    public String call() throws Exception { 
     return "Calling"; 
    } 
} 

然后,您可以安排由eeedev描述新创建FutureTask执行。

+0

链接不工作 – coder25

+0

谢谢,它现在已经修复。链接中有一个尾随的斜线 –

4

使用scheduleAtFixedRate(Runnable, long initialDelay, long period, TimeUnit timeunit),而不是schedule(Runnable task, long delay, TimeUnit timeunit)

scheduleAtFixedRate (Runnable, long initialDelay, long period, TimeUnit timeunit)
创建并执行一个给定的初始延迟之后第一启用的定期操作,随后与给定的周期;即执行initialDelay,然后initialDelay+period,然后initialDelay + 2 * period,依此类推。如果任务的任何执行遇到异常,则后续执行被禁止。否则,任务将仅通过取消或终止执行者而终止。如果任务的执行时间比其周期长,则后续执行可能会晚点,但不会同时执行。 下一次执行。

+0

我的可调用类型不能运行我返回值 – coder25

+0

@ coder25然后将你的可调用包装在['FutureTask']中(https://docs.oracle.com/javase/8/docs/api/java/ util/concurrent/FutureTask.html),它可以被调度,因为它是'Runnable'。 – zapl

+0

我没有让你如何在未来的任务中包装callable – coder25