CountDownLatch 模拟并发
2022-03-26 本文已影响0人
xiaohei_e853
@Test
public void TEST_TX() throws Exception {
int N = 2;
CountDownLatch latch = new CountDownLatch(N);
for (int i = 0; i < N; i++) {
Thread.sleep(100L);
new Thread(() -> {
try {
latch.await();
System.out.println("---> start " + Thread.currentThread().getName());
Thread.sleep(1000L);
CourseChapterLiveRecord courseChapterLiveRecord = new CourseChapterLiveRecord();
courseChapterLiveRecord.setCourseChapterId(9785454l);
courseChapterLiveRecord.setCreateTime(new Date());
courseChapterLiveRecord.setRecordEndTime(new Date());
courseChapterLiveRecord.setDuration("aaa");
courseChapterLiveRecord.setSiteDomain("ada");
courseChapterLiveRecord.setRecordId("aaaaaaaaa");
courseChapterLiveRecordServiceImpl.saveCourseChapterLiveRecord(courseChapterLiveRecord);
System.out.println("---> end " + Thread.currentThread().getName());
} catch (Exception e) {
e.printStackTrace();
}
}).start();
latch.countDown();
}
}