@Slf4j
public class SyncTest {
public void test1() {
synchronized (this) {
for (int i = 0; i < 10; i++) {
log.info("test1 {}", i);
}
}
}
public synchronized void test2() {
for (int i = 0; i < 10; i++) {
log.info("test2 {}", i);
}
}
}
子类代码
@Slf4j
public class SyncTestSub extends SyncTest{
public static void main(String[] args) {
SyncTestSub sub = new SyncTestSub();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(() ->
{
sub.test2();
}
);
executorService.execute(() ->
{
sub.test2();
}
);
executorService.execute(() ->
{
sub.test2();
}
);
executorService.shutdown();
}
}
最终执行日志
00:52:46.175 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 0
00:52:46.178 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 1
00:52:46.178 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 2
00:52:46.178 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 3
00:52:46.178 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 4
00:52:46.178 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 5
00:52:46.178 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 6
00:52:46.178 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 7
00:52:46.178 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 8
00:52:46.178 [pool-1-thread-1] INFO com.lv.demo.concurrency.sync.SyncTest - test2 9
00:52:46.178 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 0
00:52:46.178 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 1
00:52:46.179 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 2
00:52:46.179 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 3
00:52:46.179 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 4
00:52:46.179 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 5
00:52:46.179 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 6
00:52:46.179 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 7
00:52:46.179 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 8
00:52:46.179 [pool-1-thread-3] INFO com.lv.demo.concurrency.sync.SyncTest - test2 9
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 0
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 1
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 2
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 3
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 4
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 5
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 6
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 7
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 8
00:52:46.179 [pool-1-thread-2] INFO com.lv.demo.concurrency.sync.SyncTest - test2 9
求解答