请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

子类集成父类调用父类的synchronized方法,多个线程调用时还是会顺序执行啊,不需要跟老师说的需要在子类中单独声明,为什么?

@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

3回答

Jimin 2020-04-07 22:09:35

你好,目前这个例子可能表达不一定好,因为执行的太快了,可以带上sleep看下效果,太快了会导致线程不是并行执行

1 回复 有任何疑惑可以回复我~
  • 不是吧老师,我也是这种情况,子类继承了test2的同步方法
    回复 有任何疑惑可以回复我~ 2020-07-28 17:09:30
  • 而且去掉父类的同步关键字,子类的test2变成随机执行了,我觉得直接继承夫类是可以将sych关键字继承过来的
    回复 有任何疑惑可以回复我~ 2020-07-28 17:10:47
Jimin 2020-05-07 23:00:24

tsst2方法加的是类级别锁,整个变成串行,不加的话就是普通执行了

0 回复 有任何疑惑可以回复我~
爆炸油条 2020-05-07 04:28:56

我遇到了同样问题,test2的循环变量改为1000,test2 添加synchronized 后的输出顺序是正确的,不添加synchronized时的输出是乱序的

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信