public class Main { public static volatile Integer acount=0; public static Object lock = new Object(); public static void main(String[] args){ new Thread(()->{ del(30); }).start(); new Thread(()->{ for (int i = 0; i < 6; i++) { add(5); } }).start(); } public static void add(int num){ synchronized(lock){ acount+=num; System.out.println("存款:"+num); lock.notify(); } } public static void del(int num){ synchronized(lock){ while (acount<num){ try { lock.wait(); System.out.println("余额不足:"+acount); } catch (InterruptedException e) { } } acount-=num; System.out.println("取款:"+num); } } }

相关推荐

03-24 21:23
已编辑
郑州大学 Java
点赞 评论 收藏
分享
牛客网
牛客企业服务