2022.3.17
1.线程池创建方法
1.1获取接口ExecutorService的实现类ThreadPoolExecutor
ExecutorService executorService = Executors.newCachedThreadPool();
ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactory() {
int n = 1;
@Override
public Thread newThread(Runnable r) {
return new Thread(r,"线程名称: "+n++);
}
});
ExecutorService executorService = Executors.newFixedThreadPool(3);
ExecutorService executorService = Executors.newFixedThreadPool(3,new ThreadFactory() {
int n = 1;
@Override
public Thread newThread(Runnable r) {
return new Thread(r,"线程名称: "+n++);
}
});
ExecutorService executorService = Executors.newSingleThreadExecutor();
ExecutorService executorService = Executors.newSingleThreadExecutor(new ThreadFactory() {
int n = 1;
@Override
public Thread newThread(Runnable r) {
return new Thread(r,"线程名称: "+n++);
}
});
1.2 获取接口ScheduleExecutorService的实现类ThreadPoolExecutor
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
int n=1;
return new Thread(r,"线程:"+n++);
}
});
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(3);
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(3, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
int n=1;
return new Thread(r,"线程:"+n++);
}
});