實現發郵件方法,隨機返回失敗成功,50個執行緒同時呼叫sendEmail傳送1000封郵件,統計傳送失敗和成功的個數
阿新 • • 發佈:2019-02-20
public class Test1 {
public static void main(String[] args) {
int num=1000;//1000封郵件
//獲得執行緒池
ExecutorService executorService=Executors.newFixedThreadPool(50);//五十個執行緒
CountDownLatch cd=new CountDownLatch(num);
SendEmail sendEmail=new SendEmail();
Count count=new Count();
for (int i = 0; i < num; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
boolean b=sendEmail.send();
if (b) {
count.addSuc();
}else {
count.addFail();
}
cd.countDown();
}
});
}
try {
cd.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("傳送成功個數"+count.getSuc());
System.err.println("傳送失敗個數"+count.getFail());
}
}
public static void main(String[] args) {
int num=1000;//1000封郵件
//獲得執行緒池
ExecutorService executorService=Executors.newFixedThreadPool(50);//五十個執行緒
CountDownLatch cd=new CountDownLatch(num);
SendEmail sendEmail=new SendEmail();
Count count=new Count();
for (int i = 0; i < num; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
boolean b=sendEmail.send();
if (b) {
count.addSuc();
}else {
count.addFail();
}
cd.countDown();
}
});
}
try {
cd.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("傳送成功個數"+count.getSuc());
System.err.println("傳送失敗個數"+count.getFail());
}
}