测试代码
@SpringBootTest
class NatrpApplicationTests {
@Autowired
ReportService reportService;
Report report = new Report();
@Test
void contextLoads() throws InterruptedException {
report.setReportId(Long.valueOf("123456"));
reportService.insertReport(report); //异步方法
System.out.println("主线程继续");
}
}
异步方法
@Service
public class ReportServiceImpl implements ReportService {
@Autowired
private ReportMapper reportMapper;
@Override
@Async
public void insertReport(Report report) {
reportMapper.insertReport(report); //代码执行到这里就会提前结束,这一行不会执行
System.out.println("线程" + Thread.currentThread().getName());
}
}
执行结果
主线程继续