携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第4天,点击查看活动详情
方法remove(Runnable)的使用
方法remove(Runnable)可以删除尚未被执行的Runnable任务。
运行类代码如下:
@Slf4j
public class RemoveRunnableRun {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
try {
System.out.println(Thread.currentThread().getName() + " 开始时间:" + simpleDateFormat.format(new Date()));
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + " 结束时间:" + simpleDateFormat.format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
threadPoolExecutor.submit(runnable);
try {
Thread.sleep(1000);
threadPoolExecutor.remove(runnable);
log.info("任务正在执行不能被删除!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果如下:
pool-1-thread-1 开始时间:15:39:03 15:39:04.029 [main] INFO com.ozx.concurrentprogram.executor.controller.RemoveRunnableRun - 任务正在执行不能被删除! pool-1-thread-1 结束时间:15:39:08
当多个任务添加到线程池中,remove操作会删除哪个任务
修改运行类代码如下:
@Slf4j
public class RemoveRunnableRun {
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " 开始时间:" + simpleDateFormat.format(new Date()));
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + " 结束时间:" + simpleDateFormat.format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Runnable secondRunnable = new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " 开始时间:" + simpleDateFormat.format(new Date()));
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + " 结束时间:" + simpleDateFormat.format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
threadPoolExecutor.execute(runnable);
threadPoolExecutor.execute(secondRunnable);
try {
Thread.sleep(1000);
threadPoolExecutor.remove(secondRunnable);
log.info("任务2未在运行能被删除!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果如下:
pool-1-thread-1 开始时间:15:53:29 15:53:30.360 [main] INFO com.ozx.concurrentprogram.executor.controller.RemoveRunnableRun - 任务2未在运行能被删除! pool-1-thread-1 结束时间:15:53:34
继续修改运行类代码如下:
@Slf4j
public class RemoveRunnableRun {
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " 开始时间:" + simpleDateFormat.format(new Date()));
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + " 结束时间:" + simpleDateFormat.format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Runnable secondRunnable = new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " 开始时间:" + simpleDateFormat.format(new Date()));
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + " 结束时间:" + simpleDateFormat.format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
threadPoolExecutor.submit(runnable);
threadPoolExecutor.submit(secondRunnable);
try {
Thread.sleep(1000);
threadPoolExecutor.remove(secondRunnable);
log.info("main线程 结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果如下:
pool-1-thread-1 开始时间:16:08:12
16:08:13.711 [main] INFO com.ozx.concurrentprogram.executor.controller.RemoveRunnableRun - main线程 结束
pool-1-thread-1 结束时间:16:08:17
pool-1-thread-1 开始时间:16:08:17
pool-1-thread-1 结束时间:16:08:22
从上述运行结果看出使用execute方法进行添加需要执行的任务,因为任务2并未运行,所以任务2被remove()方法成功删除,但使用submit()方法提交的任务未被执行时,使用remove()方法却不能删除此任务,所以任务2没有被删除。