2016-11-24 100 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