在超市系统添加模拟售卖活动

该代码示例展示了如何使用Java的HashMap实现商品仓库管理和售卖。通过创建多个线程,模拟了商家售卖和用户抢购商品的过程,涉及到线程间的共享数据及并发控制。
摘要由CSDN通过智能技术生成
import java.util.HashMap;

//这里是商品仓库
public class GoodsStash implements Runnable{
    HashMap<Integer,String> sp=new HashMap<>();

    @Override
    public void run() {
        for (int i=0;i<100;i++){
            if (sp.get(i)!=null) {
                System.out.println("售卖" + sp.get(i));
            }else {
                System.out.println("目前没有商品");
            }
        }
    }
    public void warehousing(){
        for (int i=0;i<100;i++){
            sp.put(i,"商品--->"+i);
            System.out.println("商品--->"+i+"入库");
        }

    }
}

添加GoodsStash作为可以操作的资源

用一个HashMap数组储存商品资源

warehousing()方法添加商品资源

run()方法消耗商品

public class Activity618 {
    public void Activity618(){
        GoodsStash gs=new GoodsStash();
        Thread t=new Thread(gs);

        t.start();
        gs.warehousing();
    }
}

在Activity618中开一个分支栈进行商品售卖主栈进行入库添加商品

import java.util.HashMap;

public class Activity618 {
    public void Activity618(){
        HashMap<Integer,String> sp=new HashMap<>();
//        GoodsStash gs=new GoodsStash();
//        Thread t=new Thread(gs);
//
//        t.start();
//        gs.warehousing();
       Thread t=new Thread(){
           @Override
           public void run() {
               for (int i=0;i<100;i++){
                   if (sp.get(i)!=null) {
                       System.out.println("商家"+"售卖" + sp.get(i));

                   }else {
                       System.out.println("目前没有商品");
                   }
               }
           }
       };
       t.start();
        for (int i=0;i<100;i++){
            sp.put(i,"商品--->"+i);
            System.out.println("商品--->"+i+"入库");
        }
    }
}

当然我们可以利用匿名内部类都写在Activity618中

import java.util.HashMap;

public class Activity618 {
    public void Activity618(){
        HashMap<Integer,String> sp=new HashMap<>();
//        GoodsStash gs=new GoodsStash();
//        Thread t=new Thread(gs);
//
//        t.start();
//        gs.warehousing();
       Thread t=new Thread(){
           @Override
           public void run() {
               for (int i=0;i<100;i++){
                   if (sp.get(i)!=null) {
                       System.out.println("商家1号正在售卖" + sp.get(i));

                   }else {
                       System.out.println("目前没有商品");
                   }
               }
           }
       };
        Thread t1=new Thread(){
            @Override
            public void run() {
                for (int i=0;i<100;i++){
                    if (sp.get(i)!=null) {
                        System.out.println("商家2号正在售卖" + sp.get(i));

                    }else {
                        System.out.println("目前没有商品");
                    }
                }
            }
        };
       t.start();
       t1.start();
        for (int i=0;i<100;i++){
            sp.put(i,"商品--->"+i);
            System.out.println("商品--->"+i+"入库");
        }
    }
}

然后我们可以开辟两个分支栈共同售卖商品

import java.util.HashMap;

public class Activity618 {
    public void Activity618(){
        HashMap<Integer,String> sp=new HashMap<>();//总仓库
        HashMap<Integer,String> sp1=new HashMap<>();//用户仓库
        boolean goodsifsale=false;
//        GoodsStash gs=new GoodsStash();
//        Thread t=new Thread(gs);
//
//        t.start();
//        gs.warehousing();
       Thread t=new Thread(){
           @Override
           public void run() {
               for (int i=0;i<100;i++){
                   if (sp.get(i)!=null) {
                       System.out.println("商家1号正在售卖" + sp.get(i));
sp.remove(i);sp1.put(i,"商品--->"+i);
                   }else {
                       System.out.println("目前没有商品");
                   }
               }
           }
       };
        Thread t1=new Thread(){
            @Override
            public void run() {
                for (int i=0;i<100;i++){
                    if (sp.get(i)!=null) {
                        System.out.println("商家2号正在售卖" + sp.get(i));
                        sp.remove(i);sp1.put(i,"商品--->"+i);
                    }else {
                        System.out.println("目前没有商品");
                    }
                }
            }
        };
       t.start();
       t1.start();
        for (int i=0;i<100;i++){
            sp.put(i,"商品--->"+i);
            System.out.println("商品--->"+i+"入库");
        }
        Thread u=new Thread(){
            @Override
            public void run() {
                for (int i=0;i<100;i++){
                    if (sp1.get(i)!=null) {
                        System.out.println("用户1号正在抢购" + sp1.get(i));
                        sp1.remove(i);

                    }else {
                        System.out.println("商品已出售");
                    }
                }
            }
        };
        Thread u1=new Thread(){
            @Override
            public void run() {
                for (int i=0;i<100;i++){
                    if (sp1.get(i)!=null) {
                        System.out.println("用户2号正在抢购" + sp1.get(i));
                        sp1.remove(i);

                    }else {
                        System.out.println("商品已出售");
                    }
                }
            }
        };
        for (int i=10;i>=0;i--){
            System.out.println("还有"+i+"秒");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        u.start();
        u1.start();
    }

}

添加两个商户和两个用户

用户等待10秒购买商品

商户在此之前准备商品

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值