JavaSE项目实践:红包雨-线程池/线程

一、需求

二、思路

  • 创建红包集合,红包金额随机生成
  • 继承线程类,重写构造器和run方法

三、代码

1.原始创建线程类

java 复制代码
public class GRPThread extends Thread {
    private final List<Integer> redPacket;
    private String name;
    public GRPThread(List<Integer> redPacket, String name) {
       super(name);
        this.redPacket = redPacket;
    }
    @Override
    public void run() {
        //100个人来抢集合中的钱
        String name = Thread.currentThread().getName();
        while (true) {
            synchronized (redPacket) {
                if (redPacket.isEmpty()) {break;}
                //随机一个索引取钱
                int index = (int) (Math.random() * redPacket.size());
                    int money = redPacket.remove(index);
                    System.out.println(name + "抢到钱了,金额为:" + money);
                if (!redPacket.isEmpty()) {
                    System.out.println("活动结束了!");
                    break;
                }
            }
        }
    }
}
java 复制代码
public class main {
    //100个员工竞争200个红包
    public static void main(String[] args) {
        List<Integer> redPacket = getRedPacket();
        for(int i=0;i<100;i++){
            new GRPThread(redPacket,"员工"+i).start();
        }
    }
    public static List<Integer> getRedPacket() {
        //小红包在【0~30】之间,总占比80%,大红包在【31~100】之间,总占比20%
        List<Integer> redPacket = new ArrayList<>();
        for (int i = 0; i < 200; i++) {
            if (i < 160) {
                redPacket.add((int) (Math.random() * 30));
            } else {
                redPacket.add((int) (Math.random() * 70) + 31);
            }
        }
        return redPacket;
    }
}

2.线程池分配

java 复制代码
@Data
public class GRPThread implements Runnable {
    private final List<Integer> redPacket;
    private String name;
    public GRPThread(List<Integer> redPacket, String name) {
        this.redPacket = redPacket;
        this.name = name;
    }
    @Override
    public void run() {
        //100个人来抢集合中的钱
       // String name = Thread.currentThread().getName();
        while (true) {
            synchronized (redPacket) {
                if (redPacket.isEmpty()) {break;}
                //随机一个索引取钱
                int index = (int) (Math.random() * redPacket.size());
                    int money = redPacket.remove(index);
                    System.out.println(name + "抢到钱了,金额为:" + money);
                    //抢太快了
                if (redPacket.isEmpty()) {
                    System.out.println("活动结束了!");
                    break;
                }
            }
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();//打印异常信息
            }
        }
    }
}
java 复制代码
public class main {
    //100个员工竞争200个红包
    public static void main(String[] args) {
        List<Integer> redPacket = getRedPacket();
        ThreadPoolExecutor TPE =new ThreadPoolExecutor(25, 50, 1000,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(200),
                Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
        for(int i=1;i<101;i++){
            TPE.execute(new GRPThread(redPacket,"员工"+i));
        }
    }
    public static List<Integer> getRedPacket() {
        //小红包在【0~30】之间,总占比80%,大红包在【31~100】之间,总占比20%
        List<Integer> redPacket = new ArrayList<>();
        for (int i = 0; i < 200; i++) {
            if (i < 160) {
                redPacket.add((int) (Math.random() * 30));
            } else {
                redPacket.add((int) (Math.random() * 70) + 31);
            }
        }
        return redPacket;
    }
}
相关推荐
学长毕设5 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
C^h5 小时前
c++ string
开发语言·c++
raindayinrain5 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的5 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao6 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
zzu123zsw6 小时前
从臃肿的模板调用到声明式契约:深入 UniHttp 动态代理与多渠道生命周期扩展
java·spring boot·microsoft·unihttp
学逆向的6 小时前
C 局部静态变量
c语言·开发语言·网络安全
大都督会赢的7 小时前
C++第一节课
开发语言·c++
“AI国潮设计-小江”7 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
源码集结号7 小时前
云诊所门诊系统源码,基于SpringBoot+Vue.js架构,支持多租户设计与Docker容器化部署
java·spring boot·源码·医院信息系统·门诊系统·诊所系统