synchronized锁的内容
| import java.util.concurrent.TimeUnit; |
| |
| class Test1 { |
| |
| public static void main(String[] args) { |
| Phone phone = new Phone(); |
| |
| new Thread(() -> { |
| phone.sendMsg(); |
| }, "A").start(); |
| |
| try { |
| TimeUnit.SECONDS.sleep(1); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| |
| new Thread(() -> { |
| phone.call(); |
| }, "B").start(); |
| |
| } |
| } |
| |
| |
| class Phone { |
| |
| |
| |
| public synchronized void sendMsg() { |
| try { |
| TimeUnit.SECONDS.sleep(4); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| System.out.println("发短信"); |
| } |
| |
| public synchronized void call() { |
| System.out.println("打电话"); |
| } |
| } |
| import java.util.concurrent.TimeUnit; |
| |
| class Test2 { |
| public static void main(String[] args) { |
| |
| Phone2 phone = new Phone2(); |
| |
| new Thread(() -> { |
| phone.sendMsg(); |
| }, "A").start(); |
| |
| try { |
| TimeUnit.SECONDS.sleep(1); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| |
| new Thread(() -> { |
| phone.hello(); |
| }, "B").start(); |
| |
| |
| } |
| } |
| |
| |
| class Phone2 { |
| |
| |
| public synchronized void sendMsg() { |
| try { |
| TimeUnit.SECONDS.sleep(4); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| System.out.println("发短信"); |
| } |
| |
| public synchronized void call() { |
| System.out.println("打电话"); |
| } |
| |
| |
| public void hello() { |
| System.out.println("hello"); |
| } |
| } |
| import java.util.concurrent.TimeUnit; |
| |
| class Test3 { |
| public static void main(String[] args) { |
| |
| |
| Phone3 phone1 = new Phone3(); |
| Phone3 phone2 = new Phone3(); |
| |
| new Thread(() -> { |
| phone1.sendMsg(); |
| }, "A").start(); |
| |
| try { |
| TimeUnit.SECONDS.sleep(1); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| |
| new Thread(() -> { |
| phone2.call(); |
| }, "B").start(); |
| |
| |
| } |
| } |
| |
| |
| |
| class Phone3 { |
| public static synchronized void sendMsg() { |
| try { |
| TimeUnit.SECONDS.sleep(4); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| System.out.println("发短信"); |
| } |
| |
| public static synchronized void call() { |
| System.out.println("打电话"); |
| } |
| |
| } |
| import java.util.concurrent.TimeUnit; |
| |
| class Test4 { |
| public static void main(String[] args) { |
| |
| |
| Phone4 phone1 = new Phone4(); |
| Phone4 phone2 = new Phone4(); |
| |
| new Thread(() -> { |
| phone1.sendMsg(); |
| }, "A").start(); |
| |
| try { |
| TimeUnit.SECONDS.sleep(1); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| |
| new Thread(() -> { |
| phone2.call(); |
| }, "B").start(); |
| |
| |
| } |
| } |
| |
| class Phone4 { |
| |
| |
| public static synchronized void sendMsg() { |
| try { |
| TimeUnit.SECONDS.sleep(4); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| System.out.println("发短信"); |
| } |
| |
| |
| public synchronized void call() { |
| System.out.println("打电话"); |
| } |
| |
| } |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/15348586.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步