# EasyCount **Repository Path**: dwdyoung/easy-count ## Basic Information - **Project Name**: EasyCount - **Description**: 方便计票,算票数,算平均分的工具 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-08-18 - **Last Updated**: 2024-04-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 示例代码 ``` java /** * 模拟表决 */ @SneakyThrows @Test public void testVote(){ // 测试表决场景 int total = 900; // 总人数 int option1 = 800; // 赞成数 int option2 = total - option1; // 反对数 int threadCount = 500; // 线程模拟 // 测试表决场景 // int total = 60; // 总人数 // int option1 = 60; // 赞成数 // int option2 = 30; // 反对数 // int threadCount = 20; // 20个线程模拟 String topic = "topic4"; // 当前表决的名字,此值可以用任意主键或者唯一值代替 ExecutorService executor = Executors.newFixedThreadPool(threadCount); // 建立EasyCount // EasyCount.buildEasyCount().build(); // 釋放上一次的数据 EasyCount.release(topic); // 初始化数据 EasyCount.init(topic, new ArrayList<>(), null); // 创建回调 EasyCount.registerOptionListener(topic, new OptionListener() { @Override public void onCountChanged(CountGetter getter) { CountResult result = getter // 需要什么数据,提前调用needXXX方法 .needSum() .needNewUserInfos(Void.class) .needSumOptions("赞成") .needSumOptions("反对") .needSumOptions("弃权") .get(); if(getter.isByFlush()){ log.info("当前情况为 赞成 {} 票,反对 {} 票, 弃权 {} 票", result.getSumOptions("赞成"), result.getSumOptions("反对"), result.getSumOptions("弃权")); result.getAllUserInfos(); } else { // 总人数v // log.info("回调被触发,触发时间为" + new SimpleDateFormat("HH:mm:ss").format(new Date())); // log.info("总人数:" + result.getSum()); // log.info("赞成人数:" + result.getSumOptions("赞成")); // log.info("反对人数:" + result.getSumOptions("反对")); // log.info("弃权人数:" + result.getSumOptions("弃权")); // log.info("新的表决人数:" + result.getNewUserInfos().size()); } } }); // 模拟点击赞成 for(int i = 0; i < option1; i++){ int finalI = i; executor.submit(new Runnable() { @SneakyThrows @Override public void run() { EasyCount.increase(topic, "人员id_" + finalI, new String[]{"赞成"}); // 增加演示模拟网络抖动 Thread.sleep(50); } }); } Thread.sleep(5000); log.info("模拟点击赞同完成,期望值应该是 赞成 {} 票,反对 0 票, 弃权 0 票", option1); EasyCount.flush(topic); Thread.sleep(10000); // 模拟点击反对 for(int i = 0; i < option2; i++){ int finalI = i; executor.submit(new Runnable() { @SneakyThrows @Override public void run() { EasyCount.increase(topic, "人员id_" + finalI, new String[]{"反对"}); // 增加演示模拟网络抖动 Thread.sleep(50); } }); } Thread.sleep(5000); log.info("模拟点击反对完成,暂停5秒,期望值应该是 赞成 {} 票,反对 {} 票, 弃权 0 票", option1 - option2, option2); EasyCount.flush(topic); EasyCount.release(topic); } ``` ## 模拟报到 ``` java @Data @AllArgsConstructor @ToString class TimePo { private int registerMode; private long registerTime; private int from; } /** * 模拟报到 */ @SneakyThrows @Test public void testRegister(){ // 测试表决场景 int option1 = 200; // 报到人数 int option2 = 100; // 未到人数 int total = option1 + option2; int threadCount = 20; // 20个线程模拟 String topic = "register2"; // 此值可以用任意主键或者唯一值代替 ExecutorService executor = Executors.newFixedThreadPool(threadCount); EasyCount.release(topic); // 先将全部人设置为未到 List initList = new ArrayList<>(); for(int i = 0; i < total; i++){ initList.add(new UserInfoOption("userInfo" + i, "notReg", null)); } // 初始化数据 EasyCount.init(topic, initList, null); // 创建回调 EasyCount.registerOptionListener(topic, new OptionListener() { @Override public void onCountChanged(CountGetter getter) { CountResult result = getter // 需要什么数据,提前调用needXXX方法 .needSum() .needNewUserInfos(TimePo.class) .needSumOptions("reg") .needSumOptions("notReg") .get(); // 总人数v log.info("回调被触发,触发时间为" + new SimpleDateFormat("HH:mm:ss").format(new Date())); log.info("总人数:" + result.getSum()); log.info("报到人数:" + result.getSumOptions("reg")); log.info("未到人数:" + result.getSumOptions("notReg")); log.info("新的变更人数:" + result.getNewUserInfos().size()); } }); // 模拟点击签到 for(int i = 0; i < option1; i++){ int finalI = i; executor.submit(new Runnable() { @SneakyThrows @Override public void run() { EasyCount.increase(topic, "userInfo" + finalI, "reg", new TimePo(1, System.currentTimeMillis(), 3)); // 增加演示模拟网络抖动 Thread.sleep(1000); } }); } // 等待一分钟,让程序自动结束 Thread.sleep(60000); EasyCount.release(topic); } ```