1.当两个方法都加的synchronized的时候,锁的是当前的this对象
public synchronized void sendSMS() throws Exception { //停留4秒 TimeUnit.SECONDS.sleep(4); System.out.println("------sendSMS"); } public synchronized void sendEmail() throws Exception { System.out.println("------sendEmail"); }
2.当两个方法都加的static和synchronized的时候,锁的是当前的类的class对象
class Phone { public static synchronized void sendSMS() throws Exception { //停留4秒 TimeUnit.SECONDS.sleep(4); System.out.println("------sendSMS"); } public static synchronized void sendEmail() throws Exception { System.out.println("------sendEmail"); } }