package com.lyon.juc; import java.util.concurrent.TimeUnit; //八锁 public class Test8Lock3 { public static void main(String[] args) { //两个对象 Phone3 phone = new Phone3(); Phone3 phone1 = new Phone3(); new Thread(()->{ phone.sendSms(); },"A").start(); // try { // TimeUnit.SECONDS.sleep(1); // } catch (Exception e) { // e.printStackTrace(); // } new Thread(()->{ phone1.call(); },"B").start(); } } class Phone3{ //static synchronized 静态方法 同步锁 锁的是类,无论new多少个对象 public static synchronized void sendSms(){ try { TimeUnit.SECONDS.sleep(4); } catch (Exception e) { e.printStackTrace(); } System.out.println("sendSms"); } public static synchronized void call(){ System.out.println("call"); } }
多线程8锁之静态同步锁
最新推荐文章于 2021-11-06 12:29:11 发布