package com.lyon.juc; import java.util.concurrent.TimeUnit; public class Test8Lock4 { public static void main(String[] args) { //两个对象 Phone4 phone = new Phone4(); Phone4 phone1 = new Phone4(); new Thread(()->{ phone.sendSms(); },"A").start(); // try { // TimeUnit.SECONDS.sleep(1); // } catch (Exception e) { // e.printStackTrace(); // } new Thread(()->{ phone1.call(); },"B").start(); } } class Phone4{ //static synchronized 静态方法 同步锁 锁的是类,无论new多少个对象 public static synchronized void sendSms(){ try { TimeUnit.SECONDS.sleep(4); } catch (Exception e) { e.printStackTrace(); } System.out.println("sendSms"); } //普通同步方法 public synchronized void call(){ System.out.println("call"); } }