客户推广公司,排名怎么优化快,上海软件外包公司名单,创意设计是什么在Web应用程序中使用线程并不常见#xff0c;尤其是当您必须开发长期运行的任务时。 考虑到spring#xff0c;我们必须格外注意并使用它已经提供的工具#xff0c;而不是生成我们自己的线程。 我们希望线程由spring管理#xff0c;因此能够在不产生任何影响的情况下使用应… 在Web应用程序中使用线程并不常见尤其是当您必须开发长期运行的任务时。 考虑到spring我们必须格外注意并使用它已经提供的工具而不是生成我们自己的线程。 我们希望线程由spring管理因此能够在不产生任何影响的情况下使用应用程序的其他组件并在不进行任何工作的情况下优雅地关闭应用程序。 Spring提供TaskExecutor作为与执行程序打交道的抽象。 Spring的TaskExecutor接口与java.util.concurrent.Executor接口相同。 Spring发行版中包含许多TaskExecutor的预构建实现您可以从官方文档中找到有关它们的更多信息。 通过为您的Spring环境提供TaskExecutor实现您将能够将TaskExecutor注入到您的bean中并可以访问托管线程。 package com.gkatzioura.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import java.util.List;/*** Created by gkatzioura on 4/26/17.*/
Service
public class AsynchronousService {Autowiredprivate ApplicationContext applicationContext;Autowiredprivate TaskExecutor taskExecutor;public void executeAsynchronously() {taskExecutor.execute(new Runnable() {Overridepublic void run() {//TODO add long running task}});}
} 第一步是将TaskExecutor配置添加到我们的spring应用程序中。 package com.gkatzioura.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;/*** Created by gkatzioura on 4/26/17.*/
Configuration
public class ThreadConfig {Beanpublic TaskExecutor threadPoolTaskExecutor() {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setCorePoolSize(4);executor.setMaxPoolSize(4);executor.setThreadNamePrefix(default_task_executor_thread);executor.initialize();return executor;}} 一旦我们的执行程序设置完成过程就很简单。 我们将执行程序注入到spring组件中然后提交包含要执行任务的Runnable类。 由于我们的异步代码可能还需要与应用程序的其他组件进行交互并注入它们因此一种不错的方法是创建原型范围内的可运行实例。 package com.gkatzioura;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;/*** Created by gkatzioura on 10/18/17.*/
Component
Scope(prototype)
public class MyThread implements Runnable {private static final Logger LOGGER LoggerFactory.getLogger(MyThread.class);Overridepublic void run() {LOGGER.info(Called from thread);}
} 然后我们准备将执行程序注入到我们的服务中并使用它来执行可运行实例。 package com.gkatzioura.service;import com.gkatzioura.MyThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;import java.util.List;/*** Created by gkatzioura on 4/26/17.*/
Service
public class AsynchronousService {Autowiredprivate TaskExecutor taskExecutor;Autowiredprivate ApplicationContext applicationContext;public void executeAsynchronously() {MyThread myThread applicationContext.getBean(MyThread.class);taskExecutor.execute(myThread);}} 在下一篇文章中我们将通过使用spring的异步函数将多重读取的代码库提高到一个新的水平。 您可以在github上找到源代码。 翻译自: https://www.javacodegeeks.com/2017/10/spring-threads-taskexecutor.html