Java 如何在任务完成后停止它';s超时x次
我尝试执行runnable几次,如果它在x秒内3次没有完成,我将取消它 我用来模拟需要取消任务的情况的代码如下。从输出中,我可以看到相应地抛出并捕获了InterruptedException,但任务仍在运行 似乎在抛出3次TimeoutException之前,任务运行的前两次,这两次运行会一直运行,直到完成为止。我想知道是否有办法阻止这两次跑步完成Java 如何在任务完成后停止它';s超时x次,java,multithreading,Java,Multithreading,我尝试执行runnable几次,如果它在x秒内3次没有完成,我将取消它 我用来模拟需要取消任务的情况的代码如下。从输出中,我可以看到相应地抛出并捕获了InterruptedException,但任务仍在运行 似乎在抛出3次TimeoutException之前,任务运行的前两次,这两次运行会一直运行,直到完成为止。我想知道是否有办法阻止这两次跑步完成 public class SomeClass { private static int c =0; public static void mai
public class SomeClass {
private static int c =0;
public static void main(String[] args){
Runnable dummyRunnable = new Runnable() {
@Override
public void run() {
System.out.println("Hello from dummyRunnable!");
for (int i =0; i< 10; i++){
try {
//simulate work here
if (!Thread.currentThread().isInterrupted()) Thread.sleep(5000);
System.out.println("thread sleeps for the " + i + " time!");
} catch (InterruptedException ie){
System.out.println("InterruptedException catched in dummyRunnable!");
//Thread.currentThread().interrupt(); //this has no effects
break;
}
}
}
};
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(10 * 3, true);
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 3, Long.MAX_VALUE, TimeUnit.MILLISECONDS, blockingQueue);
for (int i =0; i< 5; i++){
Future<?> task = executor.submit(dummyRunnable);
try{
Thread.sleep(1000);
task.get(2000, TimeUnit.MILLISECONDS);
} catch (TimeoutException te){
c++;
System.out.println("TimeoutException from a task!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (c==3){
System.out.println("cancelling task...");
task.cancel(true);
break;
}
}
}
}
}
公共类SomeClass{
私有静态int c=0;
公共静态void main(字符串[]args){
Runnable dummyRunnable=new Runnable(){
@凌驾
公开募捐{
System.out.println(“来自dummyRunnable的你好!”);
对于(int i=0;i<10;i++){
试一试{
//在这里模拟工作
如果(!Thread.currentThread().isInterrupted())Thread.sleep(5000);
System.out.println(“线程在“+i+”时间内休眠!”);
}捕获(中断异常ie){
System.out.println(“在dummyRunnable中捕获的中断异常!”);
//Thread.currentThread().interrupt();//这没有效果
打破
}
}
}
};
BlockingQueue BlockingQueue=new ArrayBlockingQueue(10*3,真);
ThreadPoolExecutor executor=新的ThreadPoolExecutor(3,3,Long.MAX_值,TimeUnit.millides,blockingQueue);
对于(int i=0;i<5;i++){
未来任务=executor.submit(dummyRunnable);
试一试{
睡眠(1000);
get(2000,时间单位为毫秒);
}捕获(TimeoutException te){
C++;
System.out.println(“来自任务的TimeoutException!”);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(执行例外){
//TODO自动生成的捕捉块
e、 printStackTrace();
}最后{
如果(c==3){
System.out.println(“取消任务…”);
任务。取消(true);
打破
}
}
}
}
}
我不明白你到底想模拟什么。我会期待一个模拟,比如用信用卡付款(完成一项任务需要60秒的时间),或者在医生和病人的情况下找一个秘书
按照现在的方式,您将在将来创建5个对象。
如果您希望对线程进行更多的控制,那么应该考虑使用同步方法和一个为您处理线程的监视器
通常在开始一个线程时,您应该使用
new Thread(new Task(object or generics)).start();
Thread.sleep(2000); // calls this thread to wait 2 secs before doing other task(s)
在做一些硬核并发(多线程)之前,您应该阅读一些java教程以获得一些灵感。。。
禁止操作!我们应该回答这个问题吗\