Spring线程池ThreadPoolExecutor配置并且得到任务执行的结果

网友投稿 501 2023-01-11


Spring线程池ThreadPoolExecutor配置并且得到任务执行的结果

用ThreadPoolExecutor的时候,又想知道被执行的任务的执行情况,这时就可以用FutureTask。

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;

// 保存任务所需要的数据

private Object threadPoolTaskData;

private static int consumeTaskSleepTime = 2000;

public ThreadPoolTask(Object tasks) {

this.threadPoolTaskData = tasks;

}

public synchronized String call() throws Exception {

// 处理一个任务,这里的处理方式太简单了,仅仅是一个打印语句

System.out.println("开始执行任务:" + threadPoolTaskData);

String result = "";

// //便于观察,等待一段时间

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("创建任务并提交到线程池中:" + task);

FutureTask futureTask = new FutureTask(

new ThreadPoolTask(task));

threadPoolTaskExecutor.execute(futureTask);

// 在这里可以做别的任何事情

String result = null;

try {

// 取得结果,同时设置超时执行时间为0.1秒。同样可以用future.get(),不设置执行超时时间取得结果

result = futureTask.get();

} catch (InterruptedException e) {

futureTask.cancel(true);

} catch (ExecutionException e) {

futureTask.cancel(true);

} catch (Exception e) {

futureTask.cancel(true);

// 超时后,进行相应处理

} finally {

System.out.println("task@" + i + ":result=" + result);

}

}

SPRING配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd">

destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"

p:url="jdbc:mysql://localhost:3306/mb_main?useUnicode=true&characterEncoding=UTF-8&useServerPrepStmts=true" p:username="root" p:password="1234" />

p:dataSource-ref="dataSource" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource" />

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd">

destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"

p:url="jdbc:mysql://localhost:3306/mb_main?useUnicode=true&characterEncoding=UTF-8&useServerPrepStmts=true" p:username="root" p:password="1234" />

p:dataSource-ref="dataSource" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource" />

destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"

p:url="jdbc:mysql://localhost:3306/mb_main?useUnicode=true&characterEncoding=UTF-8&useServerPrepStmts=true" p:username="root" p:password="1234" />

p:dataSource-ref="dataSource" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource" />

p:dataSource-ref="dataSource" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource" />

测试类

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()

{

// 构造一个线程池

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();

}

}

}

项目截图(基于maven构建)

运行截图:

如果遇到cpu忙执行超过1秒的会返回null

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口测试用例怎么导入禅道(禅道怎么写测试用例)
下一篇:抓包一类的属于接口测试吗(接口测试怎么抓包)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~