Java 在executor服务延迟后安排任务
我希望一个任务在延迟3秒后执行,而我的一个任务需要2秒才能完成 我得到的输出显示间隔为5秒 注意:学生类实现可调用接口 我有以下疑问Java 在executor服务延迟后安排任务,java,scheduledexecutorservice,Java,Scheduledexecutorservice,我希望一个任务在延迟3秒后执行,而我的一个任务需要2秒才能完成 我得到的输出显示间隔为5秒 注意:学生类实现可调用接口 我有以下疑问 为什么会有5秒的延迟,怎么会有3秒的延迟 秒为什么线程1在第二次执行中显示,它应该是 线二 我得到的结果是 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 He
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();
}
}
}
}
专用字符串显示(字符串名称2){
试一试{
//System.out.println(Thread.currentThread().getName());
name2=Thread.currentThread().getName()+“Hello”+name;
System.out.println(“在:“+name+”-Time-“+new Date()期间执行任务”);
睡眠(000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回名称2;
}
@凌驾
公共字符串调用()引发异常{
//TODO自动生成的方法存根
如果(名称==“archana”){
抛出新异常();
}
/*}捕获(例外e){
//TODO自动生成的捕捉块
//e.printStackTrace();
}最后{
返回“错误”;
}*/
返回显示(名称);
}
公共类ExecutorScheduleDemo{
公共静态void main(字符串args[])引发InterruptedException{
ScheduledExecutorService executor=Executors.newScheduledThreadPool(5);
ArrayList=新建ArrayList();
添加(新学生(“prerna”);
列表。添加(新学生(“abc”);
//添加(新学生(“archana”);
列表。添加(新学员(“def”);
添加(新学生(“xyz”);
列表。添加(新学生(“ritu”);
列表。添加(新学生(“babita”);
System.out.println(“时间为:+new Date());
List resultList=new ArrayList();
学生(s:名单){
未来f=执行者计划(秒,3,时间单位秒);
试一试{
System.out.println(f.get());
}捕获(执行例外){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
使用而不是计划(可运行任务、长延迟、时间单位)
scheduleAtFixedRate(可运行、长初始延迟、长周期、,
时间单位时间单位)
创建并执行一个周期性动作,该动作在给定的初始延迟后首先启用,然后在给定的时间段内启用;也就是说,执行将在
initialDelay
之后开始,然后是initialDelay+period
,然后是initialDelay+2*period
,依此类推。如果任务的任何执行遇到异常,则会抑制后续执行。否则,任务将仅通过取消或终止执行者而终止。如果此任务的任何执行时间超过其周期,则后续执行可能会延迟开始,但不会同时执行。
下一次执行
使用而不是计划(可运行任务、长延迟、时间单位)
scheduleAtFixedRate(可运行、长初始延迟、长周期、,
时间单位时间单位)
创建并执行一个周期性动作,该动作在给定的初始延迟后首先启用,然后在给定的时间段内启用;也就是说,执行将在
initialDelay
之后开始,然后是initialDelay+period
,然后是initialDelay+2*period
,依此类推。如果任务的任何执行遇到异常,则会抑制后续执行。否则,任务将仅通过取消或终止执行者而终止。如果此任务的任何执行时间超过其周期,则后续执行可能会延迟开始,但不会同时执行。
下一次执行
要完成eeedev的回答,因为您的对象似乎是可调用的: 您只需将
可调用的
传递给构造函数,即可创建一个新的FutureTask
,如中所述
请注意,FutureTask的类型参数必须与可调用的类型参数相同
例如:
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";
}
}
主类{
公共静态void main(字符串[]args){
Foo-Foo=新的Foo();
FutureTask footureTask=新的FutureTask(foo);
}
}
类Foo实现了可调用的{
@凌驾
公共字符串调用()引发异常{
返回“呼叫”;
}
}
然后,您可以计划新创建的
未来任务执行,如eeedev所述。以完成eeedev的回答,因为您的对象似乎是可调用的:
您只需将可调用的
传递给构造函数,即可创建一个新的FutureTask
,如中所述
请注意,FutureTask的类型参数必须与可调用的类型参数相同
例如:
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";
}
}
主类{
公共静态void main(字符串[]args){
Foo-Foo=新的Foo();
FutureTask footureTask=新的FutureTask(foo);
}
}
类Foo实现了可调用的{
@凌驾
公共字符串调用()引发异常{
返回“呼叫”;
}
}
然后,您可以按照eeedev所述安排新创建的FutureTask
执行。问题到底是什么?不清楚问题到底是什么?不清楚我是可调用类型不可运行我正在返回values@coder25然后将您的可调用项包装在一个中,这可以安排,因为它是一个可运行的
。我不知道如何在将来的任务中包装可调用项我的是可调用类型不可运行我正在返回values@coder25然后把你的可调用项包在一个,这可以安排,因为它是一个可运行的
。我不知道如何在将来的任务中包装callable。谢谢,它现在已经修复了。链接中有一条斜线谢谢,是f