锁
锁的对象是方法的调用者
锁的是当前的线程 睡的也是当前的方法
public class QuickSort {
public static void main(String[] args) throws InterruptedException {
Data data=new Data();
new Thread(()->{
data.send();
}).start();
Thread.sleep(1000);
new Thread(()->{
data.getmsg();
}).start();
}
}
class Data{
public synchronized void send(){
//锁的对象是方法的调用者
//两个方法用的是一个锁 谁先拿到谁执行
System.out.println("发消息");
}
public synchronized void getmsg(){
System.out.println("收消息");
}
}