用c 实现网站开发,北京软装设计公司有哪些,专做情侣装网站,p2p网站做牛1.newFixedThreadPool
newFixedThreadPool 是 Executors 工具类提供的一个静态方法#xff0c;用于创建一个固定大小的线程池。该线程池具有固定数量的核心线程#xff0c;且没有最大线程数限制。如果所有的核心线程都在执行任务#xff0c;新任务会被放入队列中等待执行。…1.newFixedThreadPool
newFixedThreadPool 是 Executors 工具类提供的一个静态方法用于创建一个固定大小的线程池。该线程池具有固定数量的核心线程且没有最大线程数限制。如果所有的核心线程都在执行任务新任务会被放入队列中等待执行。这种线程池适用于需要控制并发线程数量的场景而且相比于动态调整线程数量的线程池更容易掌控。
public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueueRunnable());
}public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueueRunnable(),threadFactory);
}corePoolSize核心线程数nThreads(自定义)maximumPoolSize最大线程数0keepAliveTime线程空闲时间0unit时间单位忽略workQueue任务队列无界队列LinkedBlockingQueuethreadFactory线程工厂threadFactory或defaultThreadFactoryhandler拒绝策略AbortPolicy 是默认的拒绝策略它会抛出 RejectedExecutionException 异常表示拒绝接受新任务。
2.newSingleThreadExecutor
newSingleThreadExecutor 是 Executors 工具类提供的一个静态方法用于创建一个只有一个工作线程的线程池。该线程池的核心线程数和最大线程数都为1因此它只能顺序执行任务不会并发执行。这种线程池适用于需要顺序执行任务的场景确保任务按照提交的顺序被执行。
public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueueRunnable()));
}corePoolSize核心线程数1maximumPoolSize最大线程数1keepAliveTime线程空闲时间0unit时间单位忽略workQueue任务队列无界队列LinkedBlockingQueuethreadFactory线程工厂threadFactory或defaultThreadFactoryhandler拒绝策略AbortPolicy 是默认的拒绝策略它会抛出 RejectedExecutionException 异常表示拒绝接受新任务。
3.newCachedThreadPool
newCachedThreadPool 是 Executors 工具类提供的一个静态方法用于创建一个具有自动调整大小的线程池。该线程池的核心线程数为0最大线程数为 Integer.MAX_VALUE线程空闲时间为60秒。这种线程池适用于执行很多短期异步任务的场景其中线程池的大小需要根据当前任务的数量进行动态调整。
public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueueRunnable());
}corePoolSize核心线程数0maximumPoolSize最大线程数Integer.MAX_VALUEkeepAliveTime线程空闲时间60unit时间单位sworkQueue任务队列直接提交队列SynchronousQueuethreadFactory线程工厂threadFactory或defaultThreadFactoryhandler拒绝策略AbortPolicy 是默认的拒绝策略它会抛出 RejectedExecutionException 异常表示拒绝接受新任务。
4.newScheduledThreadPool
newScheduledThreadPool 是 Executors 工具类提供的一个静态方法用于创建一个具有固定大小的、支持定时及周期性任务执行的线程池。该线程池的核心线程数是固定的而且线程池支持定时任务ScheduledTask的执行例如定时执行任务或以固定的周期执行任务。
public ScheduledThreadPoolExecutor(int corePoolSize) {super(corePoolSize, Integer.MAX_VALUE,DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,new DelayedWorkQueue());
}corePoolSize核心线程数corePoolSizemaximumPoolSize最大线程数Integer.MAX_VALUEkeepAliveTime线程空闲时间10unit时间单位MILLISECONDSworkQueue任务队列DelayedWorkQueuethreadFactory线程工厂threadFactory或defaultThreadFactoryhandler拒绝策略AbortPolicy 是默认的拒绝策略它会抛出 RejectedExecutionException 异常表示拒绝接受新任务。
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;public class ScheduledThreadPoolExample {public static void main(String[] args) {// 创建一个具有固定大小的、支持定时及周期性任务执行的线程池大小为3ScheduledExecutorService executor Executors.newScheduledThreadPool(3);// 定时执行任务executor.schedule(() - {System.out.println(Task 1 is running on thread Thread.currentThread().getName());}, 2, TimeUnit.SECONDS);// 周期性执行任务每隔3秒执行一次executor.scheduleAtFixedRate(() - {System.out.println(Task 2 is running on thread Thread.currentThread().getName());}, 0, 3, TimeUnit.SECONDS);// 关闭线程池executor.shutdown();}
}
DelayedWorkQueue
DelayedWorkQueue 并不是一个具体的线程池工作队列的类而是一个接口属于 Java 中的 java.util.concurrent 包。该接口继承自 BlockingQueue 接口用于表示支持延迟元素的工作队列。
在 Java 中延迟队列DelayedQueue是一种特殊的队列其中的元素只有在其指定的延迟时间过去后才能够被消费。DelayedWorkQueue 接口定义了对延迟元素进行操作的方法常见的实现类是 DelayQueue。
以下是 DelayedWorkQueue 接口的主要方法
put(E e)
将指定的元素插入到队列中如果需要的话将阻塞等待直到元素可用或者队列被关闭。poll()获取并移除队列中的头元素如果队列为空则返回 null。poll(long timeout, TimeUnit unit)获取并移除队列中的头元素如果队列为空则等待指定的时间如果超时还未有可用元素则返回 null。peek()获取但不移除队列的头元素如果队列为空则返回 null。size()返回队列中的元素数量。
DelayedWorkQueue 主要用于在具有时间限制的任务调度场景中例如实现定时任务的执行。实现了 Delayed 接口的元素必须提供一个 getDelay 方法该方法返回该元素还需要延迟的时间。