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

Java ExecutorServic線程池異步實(shí)現(xiàn)流程

 更新時(shí)間:2020年12月01日 11:37:04   作者:手撕高達(dá)的村長(zhǎng)  
這篇文章主要介紹了Java ExecutorServic線程池異步實(shí)現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

相信大家都在項(xiàng)目中遇到過這樣的情況,前臺(tái)需要快速的顯示,后臺(tái)還需要做一個(gè)很大的邏輯。比如:前臺(tái)點(diǎn)擊數(shù)據(jù)導(dǎo)入按鈕,按鈕后的服務(wù)端執(zhí)行邏輯A,和邏輯B(執(zhí)行大量的表數(shù)據(jù)之間的copy功能),而這時(shí)前臺(tái)不能一直等著,要返回給前臺(tái),告訴正在處理中就行了。這里就需要用到異步了。

點(diǎn)擊按鈕 -> 邏輯A ->邏輯B(異步) -> 方法結(jié)束。

到底,項(xiàng)目需求明確了,就引入了ExecutorServic線程池。

Java通過Executors提供四種線程池,分別為:

  • newCachedThreadPool創(chuàng)建一個(gè)可緩存線程池,如果線程池長(zhǎng)度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
  • newFixedThreadPool 創(chuàng)建一個(gè)定長(zhǎng)線程池,可控制線程最大并發(fā)數(shù),超出的線程會(huì)在隊(duì)列中等待。
  • newScheduledThreadPool 創(chuàng)建一個(gè)定長(zhǎng)線程池,支持定時(shí)及周期性任務(wù)執(zhí)行。
  • newSingleThreadExecutor 創(chuàng)建一個(gè)單線程化的線程池,它只會(huì)用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行。
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author szy 
 * @version 創(chuàng)建時(shí)間:2018-5-20 上午10:25:06
 * 
 */
public class Testasync {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    if(task0() == true){
      System.out.println("執(zhí)行完畢,看異步結(jié)果");
    }
    
  }

  
  public static void task1(){
    System.out.println("task1 is start");
  }
  
  public static void task2(){
     ExecutorService executor = Executors.newFixedThreadPool(1);
     executor.submit(new Callable(){

      @Override
      public Object call() throws Exception {
        // TODO Auto-generated method stub
        
        //增加睡眠時(shí)間,便于查看結(jié)果
        /* try {
            Thread.sleep(10000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }*/
         
        //異步提交
        int sum = 0;
        for (int i = 0; i < 10000; i++) {
           sum += i;
        }
        System.out.println("task2執(zhí)行數(shù)據(jù)的大量導(dǎo)入或者導(dǎo)出");
        System.out.println("task2="+sum);
        System.out.println("task2導(dǎo)入或者導(dǎo)出完成");
        return null;
      }
       
       
     });
    
  }
  
  public static void task3(){
    System.out.println("task3 is start");
    int j = 0;
    while(true) {
      if(j++ > 10) {
        break;
      }
      System.out.println("------------task3 end-----------");
    }
  }
  
  public static boolean task0(){
    task1();
    task2();
    task3();
    return true;
  }
}

然后看結(jié)果:

task1 is start
task3 is start
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
task2執(zhí)行數(shù)據(jù)的大量導(dǎo)入或者導(dǎo)出
執(zhí)行完畢,看異步結(jié)果
task2=49995000
task2導(dǎo)入或者導(dǎo)出完成

可以看出,task1 和task3先執(zhí)行了,并且方法在沒有等待task2的情況下,直接結(jié)束了。

異步的task2另開了一個(gè)線程,自己在執(zhí)行。和主線程已經(jīng)無關(guān)了。

不過,這種在eclipse中以deubug模式是看不出來的。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論