- AbortPolicy(默认策略)
- 作用:当任务无法被执行时,直接抛出RejectedExecutionException异常。
- 适用场景:适用于需要立即反馈任务执行失败信息的场景,比如在一些对任务执行成功率要求极高,不允许任务默默丢弃的场景。
- 代码示例:
import java.util.concurrent.*;
public class ThreadPoolExample {
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(
2,
2,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1),
new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 4; i++) {
executor.submit(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executor.shutdown();
}
}
- CallerRunsPolicy
- 作用:当任务无法被执行时,该任务将由提交任务的线程来执行。这样做可以减少新任务的提交速度,给线程池一些时间来处理任务。
- 适用场景:适用于对任务处理实时性要求不是特别高,并且提交任务的线程有能力处理任务的场景。比如在一些轻量级的应用中,主线程有空闲时间来处理额外任务。
- 代码示例:
import java.util.concurrent.*;
public class ThreadPoolExample {
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(
2,
2,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1),
new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 4; i++) {
executor.submit(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executor.shutdown();
}
}
- DiscardPolicy
- 作用:当任务无法被执行时,直接丢弃该任务,不做任何处理。
- 适用场景:适用于那些对任务执行结果不是特别关注,允许任务丢失的场景。比如在一些日志记录任务等,偶尔丢失一两条日志记录对整体业务影响不大。
- 代码示例:
import java.util.concurrent.*;
public class ThreadPoolExample {
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(
2,
2,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1),
new ThreadPoolExecutor.DiscardPolicy());
for (int i = 0; i < 4; i++) {
executor.submit(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executor.shutdown();
}
}
- DiscardOldestPolicy
- 作用:当任务无法被执行时,会丢弃阻塞队列中等待时间最长的任务,然后尝试提交当前任务。
- 适用场景:适用于希望优先处理最新提交的任务,对旧任务的处理优先级较低的场景。比如在一些实时数据处理场景中,更关注最新的数据。
- 代码示例:
import java.util.concurrent.*;
public class ThreadPoolExample {
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(
2,
2,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1),
new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 0; i < 4; i++) {
executor.submit(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executor.shutdown();
}
}