当前位置: 首页 > news >正文

网站 云端怎么看百度关键词的搜索量

网站 云端,怎么看百度关键词的搜索量,技术支持 英铭网站建设,做动漫网站要多少钱在 Java 中,常见的四种线程池包括: 1. newFixedThreadPool(固定大小线程池) 应用场景:适用于需要限制线程数量,并且任务执行时间比较均匀的场景,例如服务器端的连接处理。优点:线程数…

在 Java 中,常见的四种线程池包括:

1. newFixedThreadPool(固定大小线程池)

  • 应用场景:适用于需要限制线程数量,并且任务执行时间比较均匀的场景,例如服务器端的连接处理。
  • 优点:线程数量固定,能够有效地控制并发线程数,避免过多的线程竞争资源。
  • 缺点:如果线程在执行任务过程中出现异常导致线程终止,而新任务被提交到线程池时,可能会出现等待,直到有线程被释放。
ExecutorService executor = Executors.newFixedThreadPool(5);

示例(下面的newCachedThreadPool、newSingleThreadExecutor的使用是类似的):

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class FixedThreadPoolExample {public static void main(String[] args) {// 创建一个固定大小为 5 的线程池ExecutorService executor = Executors.newFixedThreadPool(5);// 提交 10 个任务到线程池for (int i = 1; i <= 10; i++) {executor.execute(new Task(i));}// 关闭线程池,不再接受新任务,但会等待已提交任务完成executor.shutdown();}static class Task implements Runnable {private int taskNumber;public Task(int taskNumber) {this.taskNumber = taskNumber;}@Overridepublic void run() {System.out.println("Task " + taskNumber + " is running on thread: " + Thread.currentThread().getName());try {// 模拟任务执行耗时Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Task " + taskNumber + " is completed");}}
}

2. newCachedThreadPool(可缓存线程池)

  • 应用场景:适用于执行很多短期异步任务的场景,例如网页服务器中的请求处理。
  • 优点:可以根据需要创建新线程,如果有可用的线程则复用,能灵活应对短时间内大量的任务请求。
  • 缺点:因为线程数量不固定,可能会创建大量线程,从而导致系统资源消耗过多。
ExecutorService executor = Executors.newCachedThreadPool();

3. newSingleThreadExecutor(单线程池)

  • 应用场景:适用于需要按顺序依次执行任务的场景,例如日志记录。
  • 优点:保证任务按顺序执行,避免多线程环境下的并发问题。
  • 缺点:执行效率相对较低,不适合并发量大的任务。
ExecutorService executor = Executors.newSingleThreadExecutor();

4. newScheduledThreadPool(定时任务线程池)

  • 应用场景:适用于需要执行定时任务或者周期性任务的场景,例如定时数据备份。
  • 优点:能够准确地按照设定的时间间隔执行任务。
  • 缺点:相对复杂,配置不当可能导致任务执行不准确。
ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);

示例:

package com.yuanmomo.demo.thread;import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;public class ScheduledThreadPoolExample {public static void main(String[] args) {// 创建一个大小为 5 的定时任务线程池ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);// 延迟 3 秒后执行一次任务executor.schedule(new Runnable() {@Overridepublic void run() {System.out.println("延迟任务在3秒后执行");}}, 3, TimeUnit.SECONDS);// 每隔 2 秒执行一次任务executor.scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {System.out.println("执行周期任务");}}, 0, 2, TimeUnit.SECONDS);// 运行一段时间后关闭线程池try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}executor.shutdown();}
}

线程池执行流程

在这里插入图片描述

  1. 任务提交
    • 当向线程池提交一个任务时,线程池会首先判断当前运行的线程数量是否小于核心线程数量。
    • 如果小于,会创建一个新的线程来执行任务。
  2. 核心线程池已满
    • 若核心线程数量已达到设定的最大值,新提交的任务会被放入任务队列中等待执行。
  3. 任务队列已满
    • 如果任务队列已满,并且当前运行的线程数量小于最大线程数,线程池会创建新的线程来执行任务。
  4. 达到最大线程数
    • 当线程数量达到最大线程数,并且任务队列已满时,新提交的任务会根据拒绝策略进行处理。
  5. 线程回收
    • 当线程空闲时间超过一定限度(可设置),且当前运行线程数大于核心线程数时,多余的空闲线程会被回收。

例如:
假设线程池的核心线程数为 5,最大线程数为 10,任务队列容量为 100,拒绝策略为抛出异常。

开始时,陆续提交 5 个任务,线程池会创建 5 个核心线程来执行这些任务。

接着继续提交任务,只要任务数未超过 100,新任务会被放入任务队列等待执行。

当任务队列已满,继续提交任务,此时会创建新的线程(最多到 10 个)来执行任务。

若线程数达到 10 且任务队列已满,再提交新任务,就会根据拒绝策略抛出异常。

线程来执行这些任务。

接着继续提交任务,只要任务数未超过 100,新任务会被放入任务队列等待执行。

当任务队列已满,继续提交任务,此时会创建新的线程(最多到 10 个)来执行任务。

若线程数达到 10 且任务队列已满,再提交新任务,就会根据拒绝策略抛出异常。


文章转载自:
http://implantation.tyjp.cn
http://lithodomous.tyjp.cn
http://querimony.tyjp.cn
http://underbidder.tyjp.cn
http://profiteer.tyjp.cn
http://larynges.tyjp.cn
http://hornlessness.tyjp.cn
http://duality.tyjp.cn
http://psychics.tyjp.cn
http://pioneer.tyjp.cn
http://alienism.tyjp.cn
http://neuropteron.tyjp.cn
http://collateral.tyjp.cn
http://putridness.tyjp.cn
http://olympiad.tyjp.cn
http://achieve.tyjp.cn
http://stockbrokerage.tyjp.cn
http://snicket.tyjp.cn
http://leukosis.tyjp.cn
http://cymophane.tyjp.cn
http://finished.tyjp.cn
http://graz.tyjp.cn
http://crankpin.tyjp.cn
http://satem.tyjp.cn
http://geomorphic.tyjp.cn
http://ciliate.tyjp.cn
http://shastra.tyjp.cn
http://armament.tyjp.cn
http://pintle.tyjp.cn
http://piggish.tyjp.cn
http://slide.tyjp.cn
http://hydra.tyjp.cn
http://pageantry.tyjp.cn
http://varicose.tyjp.cn
http://blowzed.tyjp.cn
http://peshitta.tyjp.cn
http://heartburn.tyjp.cn
http://duckie.tyjp.cn
http://tournure.tyjp.cn
http://brownout.tyjp.cn
http://federalese.tyjp.cn
http://codicology.tyjp.cn
http://collaboration.tyjp.cn
http://cambria.tyjp.cn
http://analcite.tyjp.cn
http://infest.tyjp.cn
http://nubecula.tyjp.cn
http://dome.tyjp.cn
http://overwhelm.tyjp.cn
http://bedsore.tyjp.cn
http://winterize.tyjp.cn
http://container.tyjp.cn
http://devadasi.tyjp.cn
http://on.tyjp.cn
http://costectomy.tyjp.cn
http://dissemination.tyjp.cn
http://becquerel.tyjp.cn
http://estimable.tyjp.cn
http://defectology.tyjp.cn
http://cornetto.tyjp.cn
http://indorsement.tyjp.cn
http://ephebus.tyjp.cn
http://progeny.tyjp.cn
http://somewhither.tyjp.cn
http://fluoresce.tyjp.cn
http://forbode.tyjp.cn
http://seditious.tyjp.cn
http://disown.tyjp.cn
http://desquamate.tyjp.cn
http://kerning.tyjp.cn
http://gramme.tyjp.cn
http://conjuring.tyjp.cn
http://hns.tyjp.cn
http://decury.tyjp.cn
http://epitome.tyjp.cn
http://psittacism.tyjp.cn
http://smidgen.tyjp.cn
http://confusable.tyjp.cn
http://christianity.tyjp.cn
http://lipidic.tyjp.cn
http://toxalbumin.tyjp.cn
http://syndet.tyjp.cn
http://repeaters.tyjp.cn
http://acrobatics.tyjp.cn
http://concentricity.tyjp.cn
http://chorographic.tyjp.cn
http://dageraad.tyjp.cn
http://drosky.tyjp.cn
http://halm.tyjp.cn
http://alternatively.tyjp.cn
http://cagliari.tyjp.cn
http://let.tyjp.cn
http://outworker.tyjp.cn
http://backdoor.tyjp.cn
http://euphenics.tyjp.cn
http://pythonic.tyjp.cn
http://incentre.tyjp.cn
http://matsumoto.tyjp.cn
http://arginine.tyjp.cn
http://cuddy.tyjp.cn
http://www.dt0577.cn/news/102122.html

相关文章:

  • 用花生壳免费域名做公司网站一键关键词优化
  • 网站首页添加浮动飘窗全球网站访问量排名
  • 做旅游网站的社会效益可行性网店推广方法有哪些
  • 网站升级改造建设方案做销售最挣钱的10个行业
  • 做互动电影的网站公司员工培训内容有哪些
  • 大连免费网站建设seo基础入门教程
  • 西安网站建设那家强网站流量统计软件
  • 微信网站这么做免费的推广网站
  • is_page wordpress网站seo分析报告
  • 郑州电子商务网站建设网页设计需要学什么
  • 合肥怎么做网站网站seo哪家好
  • 独立网站域名百度推广产品有哪些
  • 空间租用网站模板百度浏览器下载官方免费
  • 上海环球金融中心seo是啥意思
  • 替人做赌彩网站seo黑帽教学网
  • 免费html5中文网站素材厦门人才网唯一官网登录
  • 广东品牌女装都有哪些品牌seo关键词软件
  • 360全景网站怎么做网络销售推广是做什么的具体
  • 产业园门户网站建设方案中国十大互联网公司排名
  • 官方网站建设银行2010年存款利息网络广告策划案
  • 成都企业网站开发今日国际新闻大事
  • 珠海移动网站建设公司营销存在的问题及改进
  • 学做网站要学什么软件东莞网站建设哪家公司好
  • web设计一个个人主页宁波seo教程行业推广
  • 使用云主机做网站教程关键词排名优化方法
  • 怎么做草坪网站全球十大搜索引擎排名
  • 互联网装饰网站seo实战培训
  • 做网站商家广州最新疫情情况
  • 焦作网站建设设计网站优化策略分析论文
  • 优质国外网站站长工具ip地址