欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

學(xué)生視角手把手帶你寫(xiě)Java?線程池初版

 更新時(shí)間:2022年03月21日 11:16:08   作者:摸魚(yú)打醬油  
作者是一個(gè)來(lái)自河源的大三在校生,以下筆記都是作者自學(xué)之路的一些淺薄經(jīng)驗(yàn),如有錯(cuò)誤請(qǐng)指正,將來(lái)會(huì)不斷的完善筆記,幫助更多的Java愛(ài)好者入門(mén)

Java手寫(xiě)線程池(第一代)

經(jīng)常使用線程池,故今天突發(fā)奇想,手寫(xiě)一個(gè)線程池,會(huì)有很多不足,請(qǐng)多多寬容。因?yàn)檫@也是第一代的版本,后續(xù)會(huì)更完善。

手寫(xiě)線程池-定義參數(shù)

	private final AtomicInteger taskcount=new AtomicInteger(0);
    private final AtomicInteger threadNumber=new AtomicInteger(0);
    private volatile int corePoolSize; 
    private final Set<MyThreadPoolExecutor.MyWorker> workers; 
    private final BlockingQueue<Runnable> waitingQueue; 
    private final String THREADPOOL_NAME="MyThread-Pool-";
    private volatile boolean isRunning=true; 
    private volatile boolean STOPNOW=false; 
    private final ThreadFactory threadFactory; 
  • taskcount:執(zhí)行任務(wù)次數(shù)
  • threadNumber:線程編號(hào),從0開(kāi)始依次遞增。
  • corePoolSize:核心線程數(shù)
  • workers:工作線程
  • waitingQueue:等待隊(duì)列
  • THREADPOOL_NAME:線程名稱(chēng)
  • isRunning:是否運(yùn)行
  • STOPNOW:是否立刻停止
  • threadFactory:線程工廠

手寫(xiě)線程池-構(gòu)造器

    public MyThreadPoolExecutor(int corePoolSize, BlockingQueue<Runnable> waitingQueue,ThreadFactory threadFactory) {
        this.corePoolSize=corePoolSize;
        this.workers=new HashSet<>(corePoolSize);
        this.waitingQueue=waitingQueue;
        this.threadFactory=threadFactory;
        //線程預(yù)熱
        for (int i = 0; i < corePoolSize; i++) {
            new MyWorker();
        }
    }

該構(gòu)造器作用:

1:對(duì)參數(shù)進(jìn)行賦值。

2:線程預(yù)熱。根據(jù)corePoolSize的大小來(lái)調(diào)用MyWorker的構(gòu)造器。我們可以看看MyWorker構(gòu)造器做了什么。

	final Thread thread; //為每個(gè)MyWorker

        MyWorker(){
            Thread td = threadFactory.newThread(this);
            td.setName(THREADPOOL_NAME+threadNumber.getAndIncrement());
            this.thread=td;
            this.thread.start();
            workers.add(this);
        }
  • MyWorker構(gòu)造器通過(guò)線程工廠對(duì)當(dāng)前對(duì)象生成Thread;
  • 并設(shè)置線程名為:MyThread-Pool-自增線程編號(hào);
  • 然后調(diào)用線程的start方法啟動(dòng)線程;
  • 最后存放在workers這個(gè)Set集合中,這樣就可以實(shí)現(xiàn)線程復(fù)用了。

手寫(xiě)線程池-默認(rèn)構(gòu)造器

	public MyThreadPoolExecutor(){
        this(5,new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory());
    }
  • 默認(rèn)構(gòu)造器的賦初始值:
  • corePoolSize:5
  • waitingQueue:new ArrayBlockingQueue<>(10),長(zhǎng)度為10的有限阻塞隊(duì)列
  • threadFactory:Executors.defaultThreadFactory()

手寫(xiě)線程池-execute方法

	public boolean execute(Runnable runnable)
    {
        return waitingQueue.offer(runnable);
    }
  • 本質(zhì)上其實(shí)就是把Runnable(任務(wù))放到waitingQueue中。

手寫(xiě)線程池-處理任務(wù)

	   @Override
        public void run() {
            //循環(huán)接收任務(wù)
                while (true)
                {
                    if((!isRunning&&waitingQueue.size()==0)||STOPNOW)
                    {
                        break;
                    }else {
                        Runnable runnable = waitingQueue.poll();
                        if(runnable!=null){
                            runnable.run();
                            System.out.println("task==>"+taskcount.incrementAndGet());
                        }
                    }
                }
        }

本質(zhì)上就是一個(gè)死循環(huán)接收任務(wù),退出條件如下:

  • 優(yōu)雅的退出。當(dāng)isRunning為false并且waitingQueue的隊(duì)列大小為0(也就是無(wú)任務(wù)了)
  • 暴力退出。當(dāng)STOPNOW為true,則說(shuō)明調(diào)用了shutdownNow方法
  • else語(yǔ)句塊會(huì)不斷取任務(wù),當(dāng)任務(wù)!=null時(shí)則調(diào)用run方法處理任務(wù)

手寫(xiě)線程池-優(yōu)雅關(guān)閉線程池

	public void shutdown()
    {
        this.isRunning=false;
    }

手寫(xiě)線程池-暴力關(guān)閉線程池

	public void shutdownNow()
    {
        this.STOPNOW=true;
    }

手寫(xiě)線程池-源代碼

  • 手寫(xiě)線程池類(lèi)的源代碼
package com.springframework.concurrent;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 線程池類(lèi)
 * @author 游政杰
 */
public class MyThreadPoolExecutor {

    private final AtomicInteger taskcount=new AtomicInteger(0);//執(zhí)行任務(wù)次數(shù)
    private final AtomicInteger threadNumber=new AtomicInteger(0); //線程編號(hào)
    private volatile int corePoolSize; //核心線程數(shù)
    private final Set<MyThreadPoolExecutor.MyWorker> workers; //工作線程
    private final BlockingQueue<Runnable> waitingQueue; //等待隊(duì)列
    private final String THREADPOOL_NAME="MyThread-Pool-";//線程名稱(chēng)
    private volatile boolean isRunning=true; //是否運(yùn)行
    private volatile boolean STOPNOW=false; //是否立刻停止
    private final ThreadFactory threadFactory; //線程工廠

    public MyThreadPoolExecutor(){
        this(5,new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory());
    }

    public MyThreadPoolExecutor(int corePoolSize, BlockingQueue<Runnable> waitingQueue,ThreadFactory threadFactory) {
        this.corePoolSize=corePoolSize;
        this.workers=new HashSet<>(corePoolSize);
        this.waitingQueue=waitingQueue;
        this.threadFactory=threadFactory;
        //線程預(yù)熱
        for (int i = 0; i < corePoolSize; i++) {
            new MyWorker();
        }
    }

    /**
     * MyWorker就是我們每一個(gè)線程對(duì)象
     */
    private final class MyWorker implements Runnable{

        final Thread thread; //為每個(gè)MyWorker

        MyWorker(){
            Thread td = threadFactory.newThread(this);
            td.setName(THREADPOOL_NAME+threadNumber.getAndIncrement());
            this.thread=td;
            this.thread.start();
            workers.add(this);
        }

        @Override
        public void run() {
            //循環(huán)接收任務(wù)
                while (true)
                {
                    //循環(huán)退出條件:
                    //1:當(dāng)isRunning為false并且waitingQueue的隊(duì)列大小為0(也就是無(wú)任務(wù)了),會(huì)優(yōu)雅的退出。
                    //2:當(dāng)STOPNOW為true,則說(shuō)明調(diào)用了shutdownNow方法進(jìn)行暴力退出。
                    if((!isRunning&&waitingQueue.size()==0)||STOPNOW)
                    {
                        break;
                    }else {
                        //不斷取任務(wù),當(dāng)任務(wù)!=null時(shí)則調(diào)用run方法處理任務(wù)
                        Runnable runnable = waitingQueue.poll();
                        if(runnable!=null){
                            runnable.run();
                            System.out.println("task==>"+taskcount.incrementAndGet());
                        }
                    }
                }
        }
    }

    public boolean execute(Runnable runnable)
    {
        return waitingQueue.offer(runnable);
    }
    //優(yōu)雅的關(guān)閉
    public void shutdown()
    {
        this.isRunning=false;
    }
    //暴力關(guān)閉
    public void shutdownNow()
    {
        this.STOPNOW=true;
    }
}
  • 測(cè)試使用手寫(xiě)線程池代碼
package com.springframework.test;

import com.springframework.concurrent.MyThreadPoolExecutor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;

public class ThreadPoolTest {

  public static void main(String[] args) {

    
      MyThreadPoolExecutor myThreadPoolExecutor = new MyThreadPoolExecutor
              (5,new ArrayBlockingQueue<>(6), Executors.defaultThreadFactory());

      for(int i=0;i<10;i++){

          int finalI = i;
          myThreadPoolExecutor.execute(()->{
              System.out.println(Thread.currentThread().getName()+">>>>"+ finalI);
          });

      }

      myThreadPoolExecutor.shutdown();

//      myThreadPoolExecutor.shutdownNow();



  }
}

問(wèn)題

為什么自定義線程池的execute執(zhí)行的任務(wù)有時(shí)會(huì)變少?

那是因?yàn)閣aitingQueue滿(mǎn)了放不下任務(wù)了,導(dǎo)致任務(wù)被丟棄,相當(dāng)于DiscardPolicy拒絕策略

解決辦法有:

1:設(shè)置最大線程數(shù),自動(dòng)對(duì)線程池?cái)U(kuò)容。

2:調(diào)大waitingQueue的容量capacity

最后:因?yàn)檫@是我手寫(xiě)的線程池的初代版本,基本實(shí)現(xiàn)線程池的復(fù)用功能,然而還有很多未完善,將來(lái)會(huì)多出幾篇完善后的文章,對(duì)目前手寫(xiě)的線程池進(jìn)行升級(jí)。

后續(xù)還會(huì)繼續(xù)出關(guān)于作者手寫(xiě)Spring框架,手寫(xiě)Tomcat等等框架的博文!?。。。?/p>

到此這篇關(guān)于學(xué)生視角手把手帶你寫(xiě)Java 線程池的文章就介紹到這了,更多相關(guān)Java 線程池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 生成帶Logo和文字的二維碼

    Java 生成帶Logo和文字的二維碼

    這篇文章主要介紹了Java 生成帶Logo和文字的二維碼的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-04-04
  • java代碼實(shí)現(xiàn)mysql分表操作(用戶(hù)行為記錄)

    java代碼實(shí)現(xiàn)mysql分表操作(用戶(hù)行為記錄)

    這篇文章主要介紹了java代碼實(shí)現(xiàn)mysql分表操作(用戶(hù)行為記錄),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Java應(yīng)用多機(jī)器部署解決大量定時(shí)任務(wù)問(wèn)題

    Java應(yīng)用多機(jī)器部署解決大量定時(shí)任務(wù)問(wèn)題

    這篇文章主要介紹了Java應(yīng)用多機(jī)器部署解決大量定時(shí)任務(wù)問(wèn)題,兩臺(tái)服務(wù)器同時(shí)部署了同一套代碼, 代碼中寫(xiě)有spring自帶的定時(shí)任務(wù),但是每次執(zhí)行定時(shí)任務(wù)時(shí)只需要一臺(tái)機(jī)器去執(zhí)行,需要的朋友可以參考下
    2019-07-07
  • Java實(shí)現(xiàn)圖書(shū)館借閱系統(tǒng)

    Java實(shí)現(xiàn)圖書(shū)館借閱系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖書(shū)館借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Java Fluent Mybatis 分頁(yè)查詢(xún)與sql日志輸出詳解流程篇

    Java Fluent Mybatis 分頁(yè)查詢(xún)與sql日志輸出詳解流程篇

    Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國(guó)內(nèi)又以Mybatis用的多,基于mybatis上的增強(qiáng)框架,又有mybatis plus和TK mybatis等。今天我們介紹一個(gè)新的mybatis增強(qiáng)框架 fluent mybatis關(guān)于分頁(yè)查詢(xún)、sql日志輸出流程
    2021-10-10
  • Java基礎(chǔ)之不簡(jiǎn)單的數(shù)組

    Java基礎(chǔ)之不簡(jiǎn)單的數(shù)組

    數(shù)組(Array)是有序的元素序列。 若將有限個(gè)類(lèi)型相同的變量的集合命名,那么這個(gè)名稱(chēng)為數(shù)組名。組成數(shù)組的各個(gè)變量稱(chēng)為數(shù)組的分量,也稱(chēng)為數(shù)組的元素,有時(shí)也稱(chēng)為下標(biāo)變量
    2021-09-09
  • java、android可用的rtp封包解包h264案例

    java、android可用的rtp封包解包h264案例

    這篇文章主要介紹了java、android可用的rtp封包解包h264案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析

    JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析

    這篇文章主要介紹了JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 使用java實(shí)現(xiàn)手機(jī)短信驗(yàn)證全過(guò)程

    使用java實(shí)現(xiàn)手機(jī)短信驗(yàn)證全過(guò)程

    這篇文章主要介紹了使用java實(shí)現(xiàn)手機(jī)短信驗(yàn)證全過(guò)程,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java util.List如何實(shí)現(xiàn)列表分段處理

    Java util.List如何實(shí)現(xiàn)列表分段處理

    這篇文章主要介紹了Java util.List如何實(shí)現(xiàn)列表分段處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評(píng)論