java程式碼中使用多執行緒並行

NO IMAGE

@ResponseBody
@RequestMapping(“/test”)
public String testThread(){
Date date1 = new Date();
System.out.println(“註冊使用者”);
// sendEmail();
// sendMsg();
Thread thread = new Thread(() -> sendEmail());
Thread thread2 = new Thread(() -> sendMsg());
thread.start();
thread2.start();
try {
thread.join();
thread2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Date date2 = new Date();
System.out.println(“註冊使用者總共耗時:” (date2.getTime()-date1.getTime()));
return “success”;
}

private void sendEmail(){
System.out.println(“傳送郵件開始…”);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“傳送郵件結束…”);
}

private void sendMsg(){
System.out.println(“傳送簡訊開始…”);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“傳送簡訊結束…”);
}