子类集成父类调用父类的synchronized方法,多个线程调用时还是会顺序执行啊,不需要跟老师说的需要在子类中单独声明,为什么?
来源:3-3 线程安全性-原子性-synchronized
![](http://img1.sycdn.imooc.com/user/545863cd0001b72a02200220-100-100.jpg)
imooc_lvshun
2020-04-07
@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
求解答
写回答
3回答
-
Jimin
2020-04-07
你好,目前这个例子可能表达不一定好,因为执行的太快了,可以带上sleep看下效果,太快了会导致线程不是并行执行
122020-07-28 -
Jimin
2020-05-07
tsst2方法加的是类级别锁,整个变成串行,不加的话就是普通执行了
00 -
爆炸油条
2020-05-07
我遇到了同样问题,test2的循环变量改为1000,test2 添加synchronized 后的输出顺序是正确的,不添加synchronized时的输出是乱序的
00
相似问题