新人求教了,在java后台中怎么使主线程不用等待子线程执行完,让主线程先走返回页面,我使用的接口实现的多线程,我现在的需求是主线程不用等待子线程执行完,先返回页面,让子线程在后台继续执行,小白,请教,谢谢各位了。

解决方案 »

  1.   

    看看伪代码
    main{
      //主线程去干自己的事
      Object  result = ServiceA.doSomeThing();
      new Thread(){
        public void run(){
          //子线程去干自己的事情
        }
      }.start()
      return result ;
    }
      

  2.   

     ThreadPoolExecutor tpe = new ThreadPoolExecutor(5, 10, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueDeep),new ThreadPoolExecutor.DiscardOldestPolicy());  
     String[] r = rusers.split(",");
     // 向线程池中添加任务
     for (int i = 0; i < r.length; i++){
                try{
                    Thread.sleep(1);
                    List<File> list = new ArrayList<File>();
    //根据用户id查询用户邮箱
    User u = User.dao.findFirst(
    "select user_email from oa_user where user_id=?", r[i]);
    //判断邮箱是否为空
    if (u.getStr("user_email") != null) {
    //判断是否有文件
    if (file != null) {
    list.add(fil);
    }
    int moticeid=notice.getInt("notice_id");
    //调用工具类,发送 邮件
    MailKit.send(u.getStr("user_email"), null,notice.getStr("notice_title"), getServerUrl("")
    + "/console/notice/views?notice_id="+ notice.getInt("notice_id"), list);
    }
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                while (getQueueSize(tpe.getQueue()) >= queueDeep){
                    try{
                        Thread.sleep(3000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                TaskThreadPool ttp = new TaskThreadPool(i);
                tpe.execute(ttp);
            }
            tpe.shutdown();我使用了 synchronized将Queue同步了,还是我写的不对吗?
      

  3.   

     private int index;
         public TaskThreadPool(int index){
             this.index = index;
         }
        public void run(){
            try{
                Thread.sleep(3000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }还是我把多人发送邮件写错地方了,小白请教了
      

  4.   

    private synchronized int getQueueSize(Queue queue){
            return queue.size();
        }