java多线程怎么使主线程不用等待子线程执行完,就返回页面 新人求教了,在java后台中怎么使主线程不用等待子线程执行完,让主线程先走返回页面,我使用的接口实现的多线程,我现在的需求是主线程不用等待子线程执行完,先返回页面,让子线程在后台继续执行,小白,请教,谢谢各位了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看伪代码main{ //主线程去干自己的事 Object result = ServiceA.doSomeThing(); new Thread(){ public void run(){ //子线程去干自己的事情 } }.start() return result ;} 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同步了,还是我写的不对吗? private int index; public TaskThreadPool(int index){ this.index = index; } public void run(){ try{ Thread.sleep(3000); }catch (InterruptedException e){ e.printStackTrace(); } }还是我把多人发送邮件写错地方了,小白请教了 private synchronized int getQueueSize(Queue queue){ return queue.size(); } 能否详细解释为什么打印的是A,而不是B 数据库连接异常 无法识别删除键? Java可视化的问题 现在很苦恼,大家说说自己的经验,如果自如控制窗口上众品的组件。流布局也不是很好。 请教一个乘积问题 请教字符过滤 各位高手,我用java连DB2怎么就不好使呢? 请问Java怎么编写代码访问一个网址然后在搜索栏自动的输入信息搜索啊 Java Map排序 请问如何用键盘向二维数组内的元素赋值啊? 为什么这个循环没有在执行
main{
//主线程去干自己的事
Object result = ServiceA.doSomeThing();
new Thread(){
public void run(){
//子线程去干自己的事情
}
}.start()
return result ;
}
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同步了,还是我写的不对吗?
public TaskThreadPool(int index){
this.index = index;
}
public void run(){
try{
Thread.sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}
}还是我把多人发送邮件写错地方了,小白请教了
return queue.size();
}