java启动多线程进行异步处理

public class InitListener implements ServletContextListener{

 private static Logger logger = Logger.getLogger(InitListener.class);
 // 资讯索引生成器
 private InfoIndexJob infoIndexJob;
 
 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
  
  
 }

 @Override
 public void contextInitialized(ServletContextEvent arg0) {
  
  if (null == infoIndexJob) {
   infoIndexJob = SpringContextHolder.getBean("InfoIndexJob");
   if(infoIndexJob != null){
    Thread t = new Thread(infoIndexJob);
    t.start();
   }
   logger.info("InitListener:注入资讯索引生成器InfoIndexJob!");
  }
  
 }

}

 

 

 

/**
 * 通过 Runnable接口实现
 *
 * @author wang_fan
 *
 */
@Component("InfoIndexJob")
public class InfoIndexJob implements Runnable{

 private static Logger logger = Logger.getLogger(InfoIndexJob.class);

 @Autowired
 private PSSM9001Service pssM9001Service;

 public void run() {
  logger.info("开始重建资讯信息搜索词索引");
  try {
   int counts = pssM9001Service.createInfosIndex();
   logger.info("结束重建资讯信息搜索词索引,重建" + counts + "条搜索词");
  } catch (Exception e) {
   logger.error("重建资讯信息搜索词时发生异常", e);
  }
 }

}

 

下面通过Thread类继承实现

SubjectThread subjectThread = new SubjectThread(subjectMap,ZglsscObsConstants.OBS_SUBJECT_UPDATE_3);
    subjectThread.start();

 

class SubjectThread extends Thread{
  private Map<Object, Object> subjectMap;
  private String updateType;
  
  
  public SubjectThread(Map<Object, Object> subjectMap, String updateType) {
   this.subjectMap = subjectMap;
   this.updateType = updateType;
  }


  @Override
  public void run() {
   // TODO Auto-generated method stub
   try {
    obsM1001Service.updateSubject(subjectMap,updateType);
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
 }

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页