2018年9月12日 星期三

多執行序的應用-for fun

1. 產生樹狀選單搜尋
用來執行搜尋工作的thread
public class SubChannelCategoryFinder implements Runnable {
 //private Logger logger = Logger.getLogger(getClass());
 private Logger logger = LoggerFactory.getLogger(this.getClass());
 private Integer parentId;
 private ChannelCategoryDao channelCategoryDao;
 private List<ChannelCategoryView> subChannelCategoryView;
 
 

 public List<ChannelCategoryView> getSubChannelCategoryView() {
  return subChannelCategoryView;
 }

 public void setSubChannelCategoryView(List<ChannelCategoryView> subChannelCategoryView) {
  this.subChannelCategoryView = subChannelCategoryView;
 }

 public Integer getParentId() {
  return parentId;
 }

 public void setParentId(Integer parentId) {
  this.parentId = parentId;
 }

 public SubChannelCategoryFinder(ChannelCategoryDao channelCategoryDao, Integer parentId) {
  this.channelCategoryDao = channelCategoryDao;
  this.parentId = parentId;
 }
 
 @Override
 public void run() {
  // TODO Auto-generated method stub
  
  Calendar startCal = Calendar.getInstance();
  
  logger.debug("Root finder(Id="+ parentId +") start at "
    + DateUtil.getInstance().getYYYYMMDDWithMilliFormat().format(startCal.getTime()));
  
  //because the session is out of the service control, so here we have to control the 
  //session's close manually.
  Session session = channelCategoryDao.getSession();
  try{
   subChannelCategoryView = channelCategoryDao.getChannelCategory(parentId, session); 
  }finally {
   channelCategoryDao.closeSession(session);
  }
  
  //subChannelCategoryView = indexService.getSubChannelCategory(parentId);
  if (subChannelCategoryView.size() > 0) {
   List<SubChannelCategoryFinder> subFinders = new ArrayList<SubChannelCategoryFinder>();
   
   for (ChannelCategoryView view : subChannelCategoryView) {
    //SubChannelCategoryFinder finder = new SubChannelCategoryFinder(channelCategoryDao, view.getId());
    SubChannelCategoryFinder finder = new SubChannelCategoryFinder(channelCategoryDao, view.getId());
    subFinders.add(finder);
    Thread thread = new Thread(finder);
    String finderName = view.getName() + "(id=" + view.getId() + ") subCategory finder";
    thread.setName(finderName);
    thread.setDaemon(true);
    logger.debug("subFinder " + finderName + "start at "
      + DateUtil.getInstance().getYYYYMMDDWithMilliFormat().format(new Date()));
    thread.start();
    try {
     thread.join();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   
   Calendar endCal = Calendar.getInstance();
   long difference = endCal.getTimeInMillis() - startCal.getTimeInMillis(); 
   logger.debug("all finder  has execute complete at "
     + DateUtil.getInstance().getYYYYMMDDWithMilliFormat().format(endCal.getTime()));
   logger.debug("Totally consume " + difference + " milli seconds");
   
   for (ChannelCategoryView view : subChannelCategoryView) {
    for (SubChannelCategoryFinder finder : subFinders) {
     if (view.getId().intValue() == finder.getParentId().intValue()
       && finder.getSubChannelCategoryView().size() > 0) {
      view.setSubChannelCategoryView(finder.getSubChannelCategoryView());
      break;
     }
    }
   }
  }
 }

}



叫用Finder產生Menu
public List<ChannelCategoryView> getChannelCategoryMenu(ChannelCategoryModel model) {
  // TODO Auto-generated method stub

  SubChannelCategoryFinder finder = new SubChannelCategoryFinder(channelCategoryDao,
    model.getChannelCategoryId());
  Thread thread = new Thread(finder);
  String finderName = "id=" + model.getChannelCategoryId() + "'s subCategory finder";
  thread.setName(finderName);
  thread.setDaemon(true);
  logger.info("RootId= " + model.getChannelCategoryId() + ", subCategory as follow:");
  thread.start();
  try {
   thread.join();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  List<ChannelCategoryView> result = new ArrayList<ChannelCategoryView>();
  result = finder.getSubChannelCategoryView();

  return result;
}

沒有留言:

張貼留言