1、Executor
顶层接口,只提供
void execute(Runnable command); //提供执行线程启动线程方法
2、ExecutorService
提供了
shutdown() , 阻止提交新任务
shutdownNow(),阻止提交新任务,并且终止当前所有遗留的任务
awaitTermination(long timeout, TimeUnit unit) //阻塞,等到超时,或者中断执行,一般配合shutdown使用
shutdown的使用例子
public class ExecutorServiceTest {
private static int count = 0;
private static final ExecutorService excutor = new ThreadPoolExecutor(3,10,1L, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(10));
private static final ReentrantLock lock = new ReentrantLock();
private static final CountDownLatch start = new CountDownLatch(1);
private static final CountDownLatch end = new CountDownLatch(10);
public static void main(String[] args) throws InterruptedException {
for (int i=0; i<3 ;i++){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
start.await();
end.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.lock();
try {
count = count +1;
System.out.println("执行数量..."+count);
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
});
excutor.submit(thread);
}
excutor.shutdown();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("=====");
}
});
try {
excutor.submit(thread1);
}catch (Exception e){
System.out.println("提交任务失败");
}
start.countDown();
end.await();
}
}
submit(Callable<T>task),提交任务,有返回值,比如
public static void main(String[] args) throws ExecutionException, InterruptedException {
CallAbleTask callAbleTask = new CallAbleTask();
Future <String> future = excutor.submit(callAbleTask);
System.out.println(future.get()); //阻塞等待返回
}
private static class CallAbleTask implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(6000L);
return "异步处理";
}
}
submit(Runable task ) 把任务丢到线程池中执行,又线程池管理任务
invokeAll(Collection<? extends Callable<T>> tasks) //批处理任务,返回值放到一个Future集合中
public static void main(String[] args) throws ExecutionException, InterruptedException {
List<CallAbleTask> list = new ArrayList<>();
CallAbleTask callAbleTask = new CallAbleTask();
CallAbleTask callAbleTask1 = new CallAbleTask();
list.add(callAbleTask);
list.add(callAbleTask1);
List<Future<String>> futures = excutor.invokeAll(list);
for(Future future:futures)
System.out.println(future.get());
}
private static class CallAbleTask implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(2000L);
return "异步处理";
}
}
3、ThreadPoolExecutor
线程池提供了管理线程的一组方案,比如
public void setKeepAliveTime(long time, TimeUnit unit) //设置空闲线程最大的存活时间
public boolean remove(Runnable task) //从线程池中移除任务
public void purge() //清空队列中的任务
4、ScheduledThreadPoolExecutor
这个类主要是提供一种延迟队列创建线程池的方法,当然可以直接通过ThreadPoolExecutor创建
比如创建一个延迟队列的线程池,1s后才执行任务
private static final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
public static void main(String[] args) {
executor.schedule(new Runnable() {
@Override
public void run() {
System.out.println("==============");
}
},1L,TimeUnit.SECONDS);
}
本文详细介绍了Java中线程池的使用方法,包括Executor、ExecutorService、ThreadPoolExecutor和ScheduledThreadPoolExecutor等核心接口与类的功能及应用场景。通过示例代码展示了如何配置线程池参数、提交任务、批量处理任务及实现定时任务。
170万+

被折叠的 条评论
为什么被折叠?



