lock

package lock8;

import java.util.concurrent.TimeUnit;

public class Test1 {
public static void main(String[] args) {
Phone phone = new Phone();

    new Thread(()->{
        phone.sendSms();
    },"A").start();

    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }

    new Thread(()->{
        phone.call();
    },"B").start();

}

}

class Phone{
public synchronized void sendSms(){
System.out.println("sendSms");
}
public synchronized void call(){
System.out.println("call");
}

}

posted @ 2024-12-25 15:58  whoiszqy  阅读(56)  评论(0)    收藏  举报