publicclassSafetyLock{publicstaticvoidmain(String[] args){Data data =newData();newThread(()->{for(int i =0; i <10; i++){try{
data.increment();}catch(InterruptedException e){
e.printStackTrace();}}},"A").start();newThread(()->{for(int i =0; i <10; i++){try{
data.increment();}catch(InterruptedException e){
e.printStackTrace();}}},"B").start();newThread(()->{for(int i =0; i <10; i++){try{
data.decrement();}catch(InterruptedException e){
e.printStackTrace();}}},"C").start();newThread(()->{for(int i =0; i <10; i++){try{
data.decrement();}catch(InterruptedException e){
e.printStackTrace();}}},"D").start();}}classData{privatestaticint number =0;publicsynchronizedvoidincrement()throwsInterruptedException{while(number !=0){this.wait();}
number++;System.out.println(Thread.currentThread().getName()+"=>"+ number +"\t生产");this.notifyAll();}publicsynchronizedvoiddecrement()throwsInterruptedException{while(number ==0){this.wait();}
number--;System.out.println(Thread.currentThread().getName()+"=>"+ number +"\t消费");this.notifyAll();}}
public class SafetyLock { public static void main(String[] args) { Data data = new Data(); new Thread(() -> { for (int i = 0; i < 10; i++) { try { data.increment(); .