最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果

用ThreadPoolExecutor的時(shí)候,又想知道被執(zhí)行的任務(wù)的執(zhí)行情況,這時(shí)就可以用FutureTask。

網(wǎng)站的建設(shè)成都創(chuàng)新互聯(lián)公司專注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營(yíng)網(wǎng)站定制開(kāi)發(fā).小程序定制開(kāi)發(fā),H5頁(yè)面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為紗窗等企業(yè)提供專業(yè)服務(wù)。

ThreadPoolTask

package com.paul.threadPool;
import java.io.Serializable;
import java.util.concurrent.Callable;
public class ThreadPoolTask implements Callable, Serializable {
  private static final long serialVersionUID = 0;
  // 保存任務(wù)所需要的數(shù)據(jù)
  private Object threadPoolTaskData;
  private static int consumeTaskSleepTime = 2000;
  public ThreadPoolTask(Object tasks) {
    this.threadPoolTaskData = tasks;
  }
  public synchronized String call() throws Exception {
    // 處理一個(gè)任務(wù),這里的處理方式太簡(jiǎn)單了,僅僅是一個(gè)打印語(yǔ)句
    System.out.println("開(kāi)始執(zhí)行任務(wù):" + threadPoolTaskData);
    String result = "";
    // //便于觀察,等待一段時(shí)間
    try {
//      long r = 5/0;
      for ( int i= 0 ; i< 100000000 ; i++){  
      } 
      result = "OK";
    } catch (Exception e) {
      e.printStackTrace();
      result = "ERROR";
    }
    threadPoolTaskData = null;
    return result;
  }
}

模擬客戶端提交的線程

package com.paul.threadPool;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
public class StartTaskThread implements Runnable{
 private ThreadPoolTaskExecutor threadPoolTaskExecutor;
 private int i;
 public StartTaskThread(ThreadPoolTaskExecutor threadPoolTaskExecutor,int i)
 {
 this.threadPoolTaskExecutor = threadPoolTaskExecutor;
 this.i = i;
 }
 @Override
 public synchronized void run() {
 String task = "task@ " + i;
 System.out.println("創(chuàng)建任務(wù)并提交到線程池中:" + task);
 FutureTask futureTask = new FutureTask(
 new ThreadPoolTask(task));
 threadPoolTaskExecutor.execute(futureTask);
 // 在這里可以做別的任何事情
 String result = null;
 try {
 // 取得結(jié)果,同時(shí)設(shè)置超時(shí)執(zhí)行時(shí)間為0.1秒。同樣可以用future.get(),不設(shè)置執(zhí)行超時(shí)時(shí)間取得結(jié)果
 result = futureTask.get();
 } catch (InterruptedException e) {
 futureTask.cancel(true);
 } catch (ExecutionException e) {
 futureTask.cancel(true);
 } catch (Exception e) {
 futureTask.cancel(true);
 // 超時(shí)后,進(jìn)行相應(yīng)處理
 } finally {
 System.out.println("task@" + i + ":result=" + result);
 }
}

SPRING配置文件

<?xml version="1.0" encoding="UTF-8" ?>

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

測(cè)試類

package com.paul.threadPool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
@ContextConfiguration
public class TestThreadPool extends AbstractJUnit4SpringContextTests{
 private static int produceTaskSleepTime = 10;
 private static int produceTaskMaxNumber = 1000;
 @Autowired
 private ThreadPoolTaskExecutor threadPoolTaskExecutor;
 public ThreadPoolTaskExecutor getThreadPoolTaskExecutor() {
 return threadPoolTaskExecutor;
 }
 public void setThreadPoolTaskExecutor(
 ThreadPoolTaskExecutor threadPoolTaskExecutor) {
 this.threadPoolTaskExecutor = threadPoolTaskExecutor;
 }
 @Test
 public void testThreadPoolExecutor()
 {
 // 構(gòu)造一個(gè)線程池
 final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 600,
 TimeUnit.SECONDS, new ArrayBlockingQueue(3),
 new ThreadPoolExecutor.CallerRunsPolicy());
 for (int i = 1; i <= produceTaskMaxNumber; i++) {
 try {
 Thread.sleep(produceTaskSleepTime);
 } catch (InterruptedException e1) {
 e1.printStackTrace();
 }
 new Thread(new StartTaskThread(threadPoolTaskExecutor,i)).start();
 }
 }
}

項(xiàng)目截圖(基于maven構(gòu)建)

Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果

運(yùn)行截圖:

Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果

如果遇到cpu忙執(zhí)行超過(guò)1秒的會(huì)返回null

Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接


文章名稱:Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果
文章位置:http://fisionsoft.com.cn/article/jcespg.html