[英]Get a reference to the scheduled task
如何使用Java並發API獲取對計划執行的任務的引用?
我可以做這樣的事情,並保留對myRunnable的引用:
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
SimpleRunnable myRunnable = new SimpleRunnable();
exec.scheduleAtFixedRate(myRunnable, 0, 2, TimeUnit.SECONDS);
但是,有沒有一種方法可以使用Executor服務或ScheduledFuture
獲取引用?
這個問題顯示了Future
對象的集合,並討論了它們對任務的引用,但是我看不到任何公開的API方法來公開它。
將來的引用表示異步任務的計算結果,而任務本身基本上是傳遞給scheduleAtFixedRate方法(在下面的示例中為“ myRunnable”)的可運行引用。
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
SimpleRunnable myRunnable = new SimpleRunnable();
ScheduledFuture<?> future = exec.scheduleAtFixedRate(myRunnable, 0, 2,
TimeUnit.SECONDS);
我認為您沒有辦法從執行程序本身檢索Runnable。 請詳細說明您願意實現的目標,我會盡力為您提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.