我想根据条件发送邮件
ServletMail.java
//somecodes
//this code should code should be executed in background(by threads or something)
if(cond1){
sendmail(firstcond)
}
else{
sendmail(secondcond)
}
//requestdispatcher
答案 0 :(得分:2)
您可以使用ExecutorService executorService = Executors.newFixedThreadPool(threadNumber);
。这里的threadNumber是并发使用线程。
您可以像这样使用它
if(cond1){
executorService.execute(new Runnable() {
@Override
public void run() {
sendmail(firstcond);
}
});
} else{
executorService.execute(new Runnable() {
@Override
public void run() {
sendmail(secondcond);
}
});
}
别忘了将此executorService.shutdown();
放到Web服务器关闭监听器中。