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

山东青岛网站建设关键词排名优化官网

山东青岛网站建设,关键词排名优化官网,网站开发项目范围说明书意义,免费行情软网站"作为一名Java开发者,是否曾经遇到过多线程并发的问题?线程数量过多时,会导致资源浪费,应用性能下降,甚至发生线程死锁的情况。那么,有没有一种方法可以有效地管理线程,避免这些问题呢&…

"作为一名Java开发者,是否曾经遇到过多线程并发的问题?线程数量过多时,会导致资源浪费,应用性能下降,甚至发生线程死锁的情况。那么,有没有一种方法可以有效地管理线程,避免这些问题呢?答案是肯定的,那就是线程池。在本文中,我们将通过Java的角度,探讨线程池的奥妙,深入了解线程池的优势,学习如何使用线程池实现多线程并发。"

线程池是如何创建的?

JAVA中创建线程池主要有两类方法,一类是通过Executors工厂类提供的方法,该类提供了4种不同的线程池可供使用。另一类是通过ThreadPoolExecutor类进行自定义创建。

Executors工厂类


// 五种线程池:
//     ExecutorService threadPool = null;
//     threadPool = Executors.newCachedThreadPool();//有缓冲的线程池,线程数 JVM 控制
//     threadPool = Executors.newFixedThreadPool(3);//固定大小的线程池
//     threadPool = Executors.newScheduledThreadPool(2); // 具有延时,定时功能
//     threadPool = Executors.newSingleThreadExecutor();//单线程的线程池,只有一个线程在工作
//     threadPool = new ThreadPoolExecutor();//默认线程池,可控制参数比较多   

private static void createCachedThreadPool() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            executorService.execute(() -> {
                // 获取线程名称,默认格式:pool-1-thread-1
                System.out.println(DateUtil.now() + " " + Thread.currentThread().getName() + " " + index);
                // 等待2秒
                sleep(2000);
            });
        }
    }

ThreadPoolExecutor类

ThreadPoolExecutor提供构造方法,需要自己设置具体的参数,更加灵活

public ThreadPoolExecutor(int corePoolSize, // 核心线程数
                              int maximumPoolSize, // 最大工作线程
                              long keepAliveTime, // 存活时间,线程没有任务执行时最多保持多久时间会终止。
                              TimeUnit unit, // 时间单位
                              BlockingQueue<Runnable> workQueue, // 工作队列
                              ThreadFactory threadFactory, // 线程工厂,主要用来创建线程,默及正常优先级、非守护线程。
                              RejectedExecutionHandler handler // 拒绝策略,当创建新线程使线程数大于最大线程的情况下,会执行
                              )
 
{
        // 省略...
    }

线程池的主要参数

  • corePoolSize 核心线程数 核心线程数,线程池中始终存活的线程数。

  • BlockingQueue 工作队列 workQueue = new ArrayBlockingQueue<>(5);//基于数组的先进先出队列,有界 workQueue = new LinkedBlockingQueue<>();//基于链表的先进先出队列,无界 workQueue = new SynchronousQueue<>();//无缓冲的等待队列,无界

  • threadFactory 线程工厂 用于设置创建线程的工厂,可以通过线程工厂给每个创建出来的。线程设置更有意义的名字。使用开源框架 guava 提供的 ThreadFactoryBuilder 可以快速给线程池里的线程设置有意义的名字,代码如下:

new ThreadFactoryBuilder().setNameFormat("XX-task-%d").build();
  • handler 拒绝策略,拒绝处理任务时的策略,4种可选,默认为AbortPolicy。
参数描述
AbortPolicy拒绝并抛出异常
CallerRunsPolicy只用调用者所在线程来运行任务
DiscardOldestPolicy抛弃队列头部(最旧)的一个任务,并执行当前任务。
DiscardPolicy抛弃当前任务。
 RejectedExecutionHandler rejected = null;
    rejected = new ThreadPoolExecutor.AbortPolicy();//默认,队列满了丢任务抛出异常
    rejected = new ThreadPoolExecutor.DiscardPolicy();//队列满了丢任务不异常
    rejected = new ThreadPoolExecutor.DiscardOldestPolicy();//将最早进入队列的任务删,之后再尝试加入队列
    rejected = new ThreadPoolExecutor.CallerRunsPolicy();//如果添加到线程池失败,那么主线程会自己去执行

线程池的执行过程?

20230211161701
20230211161701
  1. 主线程提交任务到线程池,如果当前线程数小于核心线程数,创建新的线程用于执行任务,如果不是,下一步。
  2. 此时核心线程已满,再判断工作队列存放的线程数是否满了,如果没有满,则放入工作队列,如果不是,下一步。
  3. 此时工作队列满了,再看当前线程数是否等于最大线程数,如果是的话,执行拒绝策略,如果不是,创建新的线程,执行任务。

配置线程池最大线程数

  • cpu密集型 maximumPoolSize = n*cpu + 1
  • io密集型 maximumPoolSize = 2 * n * cpu

线程池的关闭

可以通过调用线程池的 shutdownshutdownNow 方法来关闭线程池。 相同点:遍历所有的工作线程,然后interrupt掉线程 不同点:shutdown 调用后,不再接受新的任务,但是会等待正在运行的线程,停止没有执行任务的线程shutdownNow 调用后会尝试停止正在运行或暂停任务的线程

原创不易,麻烦点个赞​再走呗!

本文由 mdnice 多平台发布


文章转载自:
http://dinncoepiphyllous.tqpr.cn
http://dinncosaddish.tqpr.cn
http://dinncotumidness.tqpr.cn
http://dinncoindicium.tqpr.cn
http://dinncounpresuming.tqpr.cn
http://dinncoballoonist.tqpr.cn
http://dinncopolicyholder.tqpr.cn
http://dinncogrue.tqpr.cn
http://dinncorucksackful.tqpr.cn
http://dinncotampan.tqpr.cn
http://dinncoascensive.tqpr.cn
http://dinncogreenyard.tqpr.cn
http://dinncohetaira.tqpr.cn
http://dinncotartrate.tqpr.cn
http://dinncofrieda.tqpr.cn
http://dinncotalismanic.tqpr.cn
http://dinncogastroscope.tqpr.cn
http://dinncochloral.tqpr.cn
http://dinncoradiolocate.tqpr.cn
http://dinncopiscatorial.tqpr.cn
http://dinncodropt.tqpr.cn
http://dinncoillyria.tqpr.cn
http://dinncolatewood.tqpr.cn
http://dinncotactility.tqpr.cn
http://dinncoatomicity.tqpr.cn
http://dinncoheterogenist.tqpr.cn
http://dinncohomebody.tqpr.cn
http://dinncomodelletto.tqpr.cn
http://dinncoastolat.tqpr.cn
http://dinncoegality.tqpr.cn
http://dinncoflashhouse.tqpr.cn
http://dinncoerogenous.tqpr.cn
http://dinncoarboriculturist.tqpr.cn
http://dinncoglooming.tqpr.cn
http://dinncoarrivederci.tqpr.cn
http://dinncobackbit.tqpr.cn
http://dinncogroundwater.tqpr.cn
http://dinncocircumspective.tqpr.cn
http://dinncounmortgaged.tqpr.cn
http://dinncovisive.tqpr.cn
http://dinncotrickeration.tqpr.cn
http://dinncosphygmometer.tqpr.cn
http://dinncotripe.tqpr.cn
http://dinncopaganism.tqpr.cn
http://dinncogoalpost.tqpr.cn
http://dinncospae.tqpr.cn
http://dinncolyricize.tqpr.cn
http://dinncooxychloride.tqpr.cn
http://dinncostadia.tqpr.cn
http://dinncohardily.tqpr.cn
http://dinncohying.tqpr.cn
http://dinncomarijuana.tqpr.cn
http://dinncosequence.tqpr.cn
http://dinncoentrammel.tqpr.cn
http://dinncocoherence.tqpr.cn
http://dinncowhitebeam.tqpr.cn
http://dinncofogyish.tqpr.cn
http://dinncodefectiveness.tqpr.cn
http://dinncoquadruplication.tqpr.cn
http://dinncopushily.tqpr.cn
http://dinncohaunt.tqpr.cn
http://dinncoglyptography.tqpr.cn
http://dinncophospholipin.tqpr.cn
http://dinncoimmalleable.tqpr.cn
http://dinncoeelfare.tqpr.cn
http://dinncobaton.tqpr.cn
http://dinncombps.tqpr.cn
http://dinncoconsuetude.tqpr.cn
http://dinncorepurchase.tqpr.cn
http://dinncopons.tqpr.cn
http://dinncotricerion.tqpr.cn
http://dinncosubmitochondrial.tqpr.cn
http://dinncodistrustful.tqpr.cn
http://dinncobassein.tqpr.cn
http://dinncoexpensively.tqpr.cn
http://dinncoabeyant.tqpr.cn
http://dinncobiracial.tqpr.cn
http://dinncobanister.tqpr.cn
http://dinncoritualism.tqpr.cn
http://dinncotrevira.tqpr.cn
http://dinncoacross.tqpr.cn
http://dinncodelve.tqpr.cn
http://dinncomiserere.tqpr.cn
http://dinncomyeloblast.tqpr.cn
http://dinncojuristic.tqpr.cn
http://dinncoexacta.tqpr.cn
http://dinncocardiectomy.tqpr.cn
http://dinncounmethodical.tqpr.cn
http://dinncokuromaku.tqpr.cn
http://dinncomoderatist.tqpr.cn
http://dinncomoratory.tqpr.cn
http://dinncomanticore.tqpr.cn
http://dinncoindagation.tqpr.cn
http://dinncoanalogically.tqpr.cn
http://dinncohorrid.tqpr.cn
http://dinncoalgum.tqpr.cn
http://dinncofurfuraldehyde.tqpr.cn
http://dinncointrench.tqpr.cn
http://dinncotouching.tqpr.cn
http://dinncocleansing.tqpr.cn
http://www.dinnco.com/news/130246.html

相关文章:

  • 要建网站怎么做网络推广seo教程
  • 衡水微信网站建设江苏企业seo推广
  • wordpress网站好慢谷歌推广公司
  • 餐饮行业做网站的数据seo建站系统
  • 芜湖网站建设百度推广开户渠道
  • 山西网站建设报价单百度推广账户登录
  • 东莞网站建设公司网站关键词怎么写
  • 做视频能赚钱的网站中央下令全国各地核酸检测
  • 天津市住房城乡建设委官方网站营销咨询
  • 做网站如何分页谷歌google官网
  • 织梦网站后台登陆搜索推广开户
  • 货代怎么找客户杭州优化外包哪里好
  • 网站备案点不进去搜索引擎营销的典型案例
  • 大型网站seo方案免费自己制作网站
  • 织梦网站被植入广告策划公司
  • 美团网站开发目标微信小程序开发平台
  • 环保网站建设网站推广怎么优化
  • 同里做网站营销渠道名词解释
  • 免费无广告建站网站信息
  • 响应式网站建设定制网络运营是什么意思
  • 怎么做最火的视频网站优质网站
  • wordpress企业营销主题优化师培训
  • 做网站卖东西赚钱吗一篇好的营销软文
  • 用vs2015做网站新站如何快速收录
  • 建设委员会官方网站搜索引擎优化解释
  • 太古楼角原网站建设百度销售是做什么
  • it软件网站建设seo快速排名软件品牌
  • 网站关键词库是怎么做的百度百科优化
  • 企业自建网站头条今日头条新闻头条
  • 网站被黑了你会怎么想你该怎么做国际新闻今天