1.7 同步方法与非同步方法是否可以同时被调用?

/**
 * This is description.
 * 同步方法与非同步方法是否可以同时被调用?
 * @author Chris Lee
 * @date 2019/3/2 11:25
 */
public class Demo{

    public synchronized void fun1() {
        System.out.println("当前线程: " + Thread.currentThread().getName() + ", fun1 start.");
        fun2();

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("当前线程: " + Thread.currentThread().getName() + ", fun1 end.");
    }

    private void fun2() {
        System.out.println("当前线程: " + Thread.currentThread().getName() + ", fun2 start.");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("当前线程: " + Thread.currentThread().getName() + ", fun2 end.");

    }

    public static void main(String[] args){
        Demo demo = new Demo();
        new Thread(() -> demo.fun1(), "thread 1").start();

        /*
            当前线程: thread 1, fun1 start.
            当前线程: thread 1, fun2 start.(这里就说明了同步方法fun1()可以同时调用非同步方法fun2().)
            当前线程: thread 1, fun2 end.
            当前线程: thread 1, fun1 end.
         */
    }

}
说明:
  • 本篇文章如有不正确或待改进的地方, 欢迎批评和指正, 大家一同进步, 谢谢!
  • 世上有4样东西可以让世界变得更美好, 它们是: 代码(Code), 诗(Poem), 音乐(Music), 爱(Love). 如有兴趣了解更多, 欢迎光顾"我的文集"相关文章.
资料:
  1. 学习视频: https://www.bilibili.com/video/av11076511/?p=1
  2. 参考代码: https://github.com/EduMoral/edu/tree/master/concurrent/src/yxxy
  3. 我的代码: https://github.com/ChrisLeejing/learn_concurrency.git
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # Awesome Python [![Awesome](https://cdn.rawgit.com/sindr...
    emily_007阅读 2,236评论 0 3
  • 2018年1月14日 星期日 晴 最近因为工作比较忙,有两周没回老妈家了。今天上午老妈打电...
    周李扬阅读 182评论 0 0
  • (1)银城 一座亮闪闪的城市出现在伯爵和明歌的眼前。伯爵指着地图给明歌看:“银城,我们到达的第25座城。” 明歌问...
    森林中的陶熙阅读 350评论 0 3