/**
* 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). 如有兴趣了解更多, 欢迎光顾"我的文集"相关文章.
资料:
- 学习视频: https://www.bilibili.com/video/av11076511/?p=1
- 参考代码: https://github.com/EduMoral/edu/tree/master/concurrent/src/yxxy
- 我的代码: https://github.com/ChrisLeejing/learn_concurrency.git
网友评论