栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有TaskExecutor示例的Spring线程?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有TaskExecutor示例的Spring线程?

很简单 这个想法是,你有一个Bean的executor对象,该对象被传递到想要触发新任务的任何对象中(在新线程中)。令人高兴的是,你只需更改Spring配置即可修改要使用的任务执行程序类型。在下面的示例中,我将使用一些示例类(ClassWithMethodToFire)并将其包装在Runnable对象中以执行操作;你实际上还可以在自己的类中实现Runnable,然后在execute方法中调用

classWithMethodToFire.run()

这是一个非常简单的例子。

public class SomethingThatShouldHappenInAThread {     private TaskExecutor taskExecutor;     private ClassWithMethodToFire classWithMethodToFire;     public SomethingThatShouldHappenInAThread(TaskExecutor taskExecutor,   ClassWithMethodToFire classWithMethodToFire) {          this.taskExecutor = taskExecutor;          this.classWithMethodToFire = classWithMethodToFire;     }     public void fire(final SomeParameterClass parameter) {          taskExecutor.execute( new Runnable() {    public void run() {         classWithMethodToFire.doSomething( parameter );    }          });     }}

And here are the Spring beans:

<bean name="somethingThatShouldHappenInAThread" >     <constructor-arg type="org.springframework.core.task.TaskExecutor" ref="taskExecutor" />     <constructor-arg type="package.name.ClassWithMethodToFire" ref="classWithMethodToFireBean"/></bean><bean id="taskExecutor" >     <property name="corePoolSize" value="5" />     <property name="maxPoolSize" value="10" />     <property name="queueCapacity" value="25" /></bean>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/404921.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号