方法一,使用执行器
package com.admin.email.service;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class MyExecutor {private ExecutorService executor = Executors.newCachedThreadPool();public void fun() throws Exception {executor.submit(new Runnable() {@Overridepublic void run() {try {//让线程休息5秒Thread.sleep(5000);System.out.println("我休息好了~");} catch (Exception e) {throw new RuntimeException("报错啦!!");}}});}public static void main(String[] args) {MyExecutor myExecutor = new MyExecutor();try {myExecutor.fun();System.out.println("我休息一会儿~");} catch (Exception e) {throw new RuntimeException("业务程序报错啦!!");}}
}
方法二,
如果是spring,直接使用
@Async