这是我使用Future的代码片段。
v1
输出:
import java.util.concurrent.*;
import java.util.*;
public class FutureDemo{
public FutureDemo(){
/* Future */
ExecutorService service = Executors.newFixedThreadPool(2);
for ( int i=0; i<10; i++){
MyCallable myCallable = new MyCallable((long)i);
Future<Long> futureResult = service.submit(myCallable);
Long result = null;
try{
result = futureResult.get(5000, TimeUnit.MILLISECONDS);
}catch(TimeoutException e){
System.out.println("Time out after 5 seconds");
futureResult.cancel(true);
}catch(InterruptedException ie){
System.out.println("Error: Interrupted");
}catch(ExecutionException ee){
System.out.println("Error: Execution interrupted");
}
System.out.println("Result:"+result);
}
}
public static void main(String args[]){
FutureDemo fc = new FutureDemo();
}
class MyCallable implements Callable{
Long id = 0L;
public MyCallable(Long val){
this.id = val;
}
public Long call(){
return id;
}
}
}
提交所有Future任务后,我得到了结果,但程序仍然没有退出。我的光标闪烁,java进程没有退出,直到我点击 CTRL + C 。有什么问题?
编辑:除非在服务器关闭时触发关闭挂钩,否则我无法关闭执行程序服务。
答案 0 :(得分:6)
您需要致电ExecutorService
告诉@Entity
@Table(name="vehicules")
public class Vehicule {
@Id
public Long id;
public Long noParc;
public Long etat;
}
不要等待更多任务。