当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot中多线程开发的注意事项总结

spring boot中多线程开发的注意事项总结

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

springt通过任务执行器(taskexecutor)来实现多线程和并发编程。使用threadpooltaskexecutor可实现一个基于线程池的taskexecutor。而实际开发中任务一般是非阻碍的,即异步的,所以我们要在配置类中通过@enableasync 开启对异步任务的支持,并通过实际执行bean的方法中使用@async注解来声明其是一个异步任务。

基于springboot的多线程程序开发过程中,由于本身也需要注入spring容器进行管理,才能发挥springboot的优势。所以这篇文字主要用来记录开发中两者结合时需要注意的一些事项。

注意事项

第一步我们把线程类的实例注入sping容器进行管理

@configuration
@springbootapplication
@import({threadconfig.class})
public class threadapp implements commandlinerunner
{
 public static void main(string[] args) throws exception {

  applicationcontext app = springapplication.run(threadapp .class, args);
  //这里主要保存上下文对象实例,需要加上。springbootutils类网上很多,可以自己搜下
  springbootutils.setapplicationcontext(app);

 }

 //access command line arguments
 @override
 public void run(string... args) throws exception {
  //do something
 }
}

//componentscan注解会扫描com.demo.thead下,也就是多线程类所在的包下的文件
@configuration
@componentscan(basepackages = { "com.demo.thread"})
public class threadconfig{

}

这里使用springboot @import 注解,把threadconfig里扫描到的包中带注解的示例,如@component等注入到spring容器当中.

然后是线程的启动,这里在我的业务场景中有两种情况:

1、程序运行时,自动启动;

这在一般的可执行程序里面,当然可以直接在main函数里执行通过代码启动线程。但在springboot中,我们可以使用@postconstruct注解的方式,让已经注入bean容器的线程对象自启动

@component
public class demothread extends thread
{
 //注意这里,如果你没有实现把多线程类的实例注入到spring容器中,这里你是无法拿到其他自动装配的对象实例的的,这也是我们第一步的意义所在。
 @autowired
 private xxxservice xxxservice;

 @postconstruct
 public void start() {
  super.start();
 }

 public void run() {
  // ok,在这里你就可以实现线程要实现的功能逻辑了,自然也可以直接使用装配好的sevice对象实例。
  
 }
}

 2、在程序中,需要开启线程时启动,比如在从kafka接收数据,开启线程处理,当然这种情况下也需要通过第一步,把线程类实例注入到sping容器中

private taskthread thread;
 private executorservice taskpool= new threadpoolexecutor(
   5, 10, 1000,
   timeunit.milliseconds, new arrayblockingqueue<>(10),
   new threadpoolexecutor.callerrunspolicy()); 


 @kafkalistener(topics = "xxtopic")
 public void receive(consumerrecord<object, object> consumerrecord) {
   jsonobject json = json.parseobject(consumerrecord.value().tostring());
   //通过springbootutils获取线程类的实例
   thread = springbootutils.getbean(taskthread.class);
   //启动线程
   //new thread(thread).start() ; 
   //向线程对象里传值
   thread.init(i);
   //放入线程池执行
   taskpool.execute(thread);

 }
//注意这里是否添加@scope("prototype")注解
@component
@scope("prototype")
public class taskthread implements runnable{
 
 protected int value=0;

 @autowired
 private xxxservice xxxservice;
 
 //threadlocal 对象,单例模式下可以保证成员变量的线程安全和独立性。
 public threadlocal<integer> valuelocal = new threadlocal < integer > () {
  @override
  protected integer initialvalue() {
   return 0;
  }
 };

 protected static final logger log = loggerfactory.getlogger(gpstaskthread.class);
 
 @override
 public final void run() {
  try { 
   log.info(value+"");
   
  } catch (exception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
 }

 public void init(int value) {
  this.value=value;
 }
}

在这里我们需要注意,taskthread这个线程类在spirngboot中是否要添加@scope("prototype")注解设置为多例模式还是默认单例模式。

在单例模式下springbootutils.getbean(taskthread.class) 每次返回的都是同一个对象,虽然不需要每次都创建新的对象,但无法保证成员变量的线程安全,也就是说在线程池中的执行的线程,它们的value值是共享的。而多例模式下,由于每次创建的都是一个新的线程对象,则不存在上述问题。

所以在这里请大家注意无论是我上面的示例代码还是平常的web开发中,spirngboot默认为单例模式,自定义的成员变量是线程不安全的,需要通过threadlocal 或这其他方法做同步处理。

回到我们当前的业务场景,在这里我们需要每个线程处理的value值不同,互不影响,那么通过@scope("prototype")注解把taskthread设置为多例模式。

总结

通过上面的示例,我们可以看到springboot与多线程的结合还是比较简单,通过配置,我们既可以在spring容器中管理线程类,也可以在线程中使用sping容器中的对象实例。同时我们在使用的过程当中要有意识的去注意线程安全方面的问题和内部运行机制的问题。当然这里理解的还是比较浅显,如果有不正确的地方还请大家指出与海涵。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网