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

Java并發(fā)之串行線程池實例解析

 更新時間:2018年02月12日 10:28:54   作者:低調(diào)小一  
這篇文章主要介紹了Java并發(fā)之串行線程池實例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

前言

做Android的這兩年時間,通過研究Android源碼,也會Java并發(fā)處理多線程有了自己的一些理解。

那么問題來了,如何實現(xiàn)一個串行的線程池呢?

思路

何為串行線程池呢?

也就是說,我們的Runnable對象應(yīng)該有個排隊的機制,它們順序從隊列尾部進入,并且從隊列頭部選擇Runnable進行執(zhí)行。

既然我們有了思路,那我們就考慮一下所需要的數(shù)據(jù)結(jié)構(gòu)?

既然是從隊列尾部插入Runnable對象,從隊列頭部執(zhí)行Runnable對象,我們自然需要一個隊列。Java的SDK已經(jīng)給我們提供了很好的隊列數(shù)據(jù)結(jié)構(gòu),例如雙端隊列:ArrayDeque<Runnable>。

  • 因為涉及到線程的執(zhí)行,那我們首先就需要有一個合適的線程池,使用ThreadPoolExecutor類即可構(gòu)造。
  • 既然是串行執(zhí)行,那如何保持串行機制呢?我們可以通過try和finally機制,我們將傳入的Runnable對象重新封裝成一個新的Runnable對象,在新的Runnable的run方法的try塊中執(zhí)行Runnable的run方法,在finally中調(diào)用執(zhí)行隊列頭部Runnable對象出隊列,并放入線程池執(zhí)行的方法。

示例代碼

import java.util.ArrayDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by wzy on 16-1-5.
 */
public class SerialExecutor {
  private Runnable mActive;
  private ArrayDeque<Runnable> mArrayDeque = new ArrayDeque<>();

  private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
  private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
  private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
  private static final int KEEP_ALIVE = 1;
  private static final BlockingQueue<Runnable> sPoolWorkQueue =
      new LinkedBlockingDeque<>(128);
  private static final ThreadFactory sThreadFactory = new ThreadFactory() {
    private final AtomicInteger mCount = new AtomicInteger(1);
    @Override
    public Thread newThread(Runnable r) {
      return new Thread(r, "Serial thread #" + mCount.getAndIncrement());
    }
  };
  private static final ThreadPoolExecutor THREAD_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE,
      MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

  public synchronized void execute(final Runnable r) {
    mArrayDeque.offer(new Runnable() {
      @Override
      public void run() {
        try {
          r.run();
        } finally {
          scheduleNext();
        }
      }
    });
    // 第一次入隊列時mActivie為空,因此需要手動調(diào)用scheduleNext方法
    if (mActive == null) {
      scheduleNext();
    }
  }

  private void scheduleNext() {
    if ((mActive = mArrayDeque.poll()) != null) {
      THREAD_EXECUTOR.execute(mActive);
    }
  }

  public static void main(String[] args) {
    SerialExecutor serialExecutor = new SerialExecutor();
    for (int i = 0; i < 10; i ++) {
      final int j = i;
      serialExecutor.execute(new Runnable() {
        @Override
        public void run() {
          System.out.println("The num is :" + (j + 1));
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      });
    }
  }
}

執(zhí)行結(jié)果如下:

The num is :1
The num is :2
The num is :3
The num is :4
The num is :5
The num is :6
The num is :7
The num is :8
The num is :9
The num is :10

總結(jié)

以上就是本文關(guān)于Java并發(fā)之串行線程池實例解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • SpringBoot配置@Configuration注解和@bean注解

    SpringBoot配置@Configuration注解和@bean注解

    這篇文章主要介紹了SpringBoot配置@Configuration注解和@bean注解,文章圍繞主題相關(guān)內(nèi)容展開詳細介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-04-04
  • HttpClient詳細使用示例詳解

    HttpClient詳細使用示例詳解

    這篇文章主要介紹了HttpClient詳細使用示例詳解,本文給大家介紹的非常想詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • java.lang.IncompatibleClassChangeError異常的問題解決

    java.lang.IncompatibleClassChangeError異常的問題解決

    本文主要介紹了java.lang.IncompatibleClassChangeError異常的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2024-06-06
  • SpringBoot工程下Lombok的應(yīng)用教程詳解

    SpringBoot工程下Lombok的應(yīng)用教程詳解

    這篇文章主要給大家介紹了關(guān)于SpringBoot工程下Lombok應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2020-11-11
  • 字符串desede 3des加密示例分享

    字符串desede 3des加密示例分享

    這篇文章主要介紹了字符串desede 3des加密示例,大家參考使用吧
    2014-01-01
  • 解決Java項目啟動報錯:Logback?configuration?error?detected:問題

    解決Java項目啟動報錯:Logback?configuration?error?detected:問題

    這篇文章主要介紹了解決Java項目啟動報錯:Logback?configuration?error?detected:問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 詳解SpringCloud Config配置中心

    詳解SpringCloud Config配置中心

    這篇文章主要介紹了詳解SpringCloud Config配置中心,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • application.yaml與bootstrap.yaml的使用

    application.yaml與bootstrap.yaml的使用

    這篇文章主要介紹了application.yaml與bootstrap.yaml的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 詳解Mybatis-plus(MP)中CRUD操作保姆級筆記

    詳解Mybatis-plus(MP)中CRUD操作保姆級筆記

    本文主要介紹了Mybatis-plus(MP)中CRUD操作保姆級筆記,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 手寫java性能測試框架第二版

    手寫java性能測試框架第二版

    這篇文章主要為大家介紹了手寫java性能測試框架第二版實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07

最新評論