Skip to content

Commit 4d987db

Browse files
committed
使用多线程打卡
1 parent 50bcc89 commit 4d987db

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

demo-latest.jar

1.54 KB
Binary file not shown.

src/main/java/com/wmg/controller/HttpController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ public R mainHandler(@RequestParam @NotNull(message = "手机号码为空,不
146146
@GetMapping("all")
147147
public R allPush() throws InterruptedException {
148148
task.process();
149-
return R.ok();
149+
return R.ok().setMsg("全部用户手动打卡成功");
150150
}
151151

152152
@GetMapping("check")
153153
public R check() throws Exception {
154154
task.check();
155-
return R.ok();
155+
return R.ok().setMsg("全部用户是否账号失效检测成功");
156156
}
157157

158158
/**

src/main/java/com/wmg/task/Task.java

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.alibaba.fastjson.JSONObject;
66
import com.wmg.Service.ExecService;
77
import com.wmg.util.TimeUtil;
8+
import lombok.SneakyThrows;
89
import lombok.extern.slf4j.Slf4j;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.beans.factory.annotation.Value;
@@ -18,9 +19,8 @@
1819
import java.util.Date;
1920
import java.util.Objects;
2021
import java.util.Set;
21-
import java.util.concurrent.Executor;
22-
import java.util.concurrent.Executors;
23-
import java.util.concurrent.ThreadLocalRandom;
22+
import java.util.concurrent.*;
23+
import java.util.concurrent.atomic.AtomicInteger;
2424

2525
/**
2626
* Description: 多线程执行定时任务 官网:www.fhadmin.org
@@ -35,8 +35,12 @@
3535
//所有的定时任务都放在一个线程池中,定时任务启动时使用不同都线程。
3636
public class Task implements SchedulingConfigurer {
3737

38-
private final static Executor executor = Executors.newCachedThreadPool();//启用多线程
39-
int count = 0;
38+
//启用多线程
39+
final ExecutorService executor = Executors.newCachedThreadPool();
40+
//AtomicInteger用来计数
41+
AtomicInteger atomicInteger = new AtomicInteger(0);
42+
43+
volatile int count = 0;
4044

4145
//定时表达式
4246
@Value("${demo.corn}")
@@ -74,45 +78,46 @@ public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
7478
*@date: 2022/6/9 1:06
7579
*/
7680
public void process() throws InterruptedException {
77-
//将计数器归零
7881
count = 0;
7982
Set<String> keys = redisTemplate.keys("XiaoMiYunDong_*");
83+
8084
if (!Objects.isNull(keys)) {
8185
for (String str : keys) {
86+
count++;
8287
//final int j=i; //关键是这一句代码,将 i 转化为 j,这样j 还是final类型的参与线程
8388
final String key = str;
84-
count++;
85-
int number=(int)(Math.random()*(1)+1);
86-
Thread.sleep(number*1000);
87-
// executor.execute(new Runnable() {
88-
// @Override
89-
// public void run() {
90-
// }
91-
// });
92-
String phoneNumber = "";
93-
try{
94-
Object redisData = redisTemplate.opsForValue().get(key);
95-
//log.info("查询数据:{}",redisData);
96-
if (!Objects.isNull(redisData)){
97-
JSONObject responseJo = JSONObject.parseObject(redisData.toString());
89+
executor.execute(new Runnable() {
90+
@Override
91+
public void run() {
92+
String phoneNumber = "";
93+
try{
94+
Object redisData = redisTemplate.opsForValue().get(key);
95+
//log.info("查询数据:{}",redisData);
96+
if (!Objects.isNull(redisData)){
97+
JSONObject responseJo = JSONObject.parseObject(redisData.toString());
9898
// System.out.println(responseJo);
99-
phoneNumber = responseJo.getString("phoneNumber");
100-
String password = responseJo.getString("password");
101-
Integer minSteps = responseJo.getInteger("minSteps");
102-
Integer maxSteps = responseJo.getInteger("maxSteps");
103-
Integer steps = ThreadLocalRandom.current().nextInt(minSteps, maxSteps+1);
104-
execService.exec(phoneNumber,password,steps);
99+
phoneNumber = responseJo.getString("phoneNumber");
100+
String password = responseJo.getString("password");
101+
Integer minSteps = responseJo.getInteger("minSteps");
102+
Integer maxSteps = responseJo.getInteger("maxSteps");
103+
Integer steps = ThreadLocalRandom.current().nextInt(minSteps, maxSteps+1);
104+
execService.exec(phoneNumber,password,steps);
105+
// System.out.println("当前线程:"+atomicInteger.incrementAndGet()+"正在执行任务");
106+
}
107+
}catch(Exception e){
108+
System.out.println(StrUtil.format("当前账号:{}打卡失败,打卡时间为:{},异常为:{}", phoneNumber,TimeUtil.getOkDate(new Date().toString()),e.getMessage()));
109+
}
105110
}
106-
}catch(Exception e){
107-
System.out.println(StrUtil.format("当前账号:{}打卡失败,打卡时间为:{},异常为:{}", phoneNumber,TimeUtil.getOkDate(new Date().toString()),e.getMessage()));
108-
}
109-
if (count >= 30){
110-
number=(int)(Math.random()*(10)+600);
111-
System.out.println("10分钟内打卡数达到30个,解黑休眠"+number+"秒,继续打卡");
111+
});
112+
if (count == 30){
113+
int number=(int)(Math.random()*(10)+600);
114+
System.out.println("10分钟内打卡数达到30个,等待解黑休眠时间"+number+"秒后继续打卡");
112115
Thread.sleep(number*1000);
113116
count = 0;
117+
// atomicInteger.set(0);
114118
}
115119
}
120+
116121
}
117122

118123
}
@@ -137,9 +142,9 @@ public void check() throws Exception {
137142
String password = "";
138143
Integer minSteps = 1;
139144
Integer maxSteps = 100000;
145+
Object redisData = redisTemplate.opsForValue().get(key);
140146
for (int i = 0; i < 3; i++){
141147
try{
142-
Object redisData = redisTemplate.opsForValue().get(key);
143148
if (!Objects.isNull(redisData)){
144149
count++;
145150
JSONObject responseJo = JSONObject.parseObject(redisData.toString());
@@ -157,10 +162,11 @@ public void check() throws Exception {
157162
flag++;
158163
}
159164

160-
int number=(int)(Math.random()*(2)+1);
161-
System.out.println(StrUtil.format("随机休眠{}秒,继续执行失效检测操作",number));
162-
Thread.sleep(number*1000);
165+
// int number=(int)(Math.random()*(2)+1);
166+
// System.out.println(StrUtil.format("随机休眠{}秒,继续执行失效检测操作",number));
167+
// Thread.sleep(number*1000);
163168
}
169+
164170
if (flag == 3){
165171
System.out.println(StrUtil.format("当前账号:{},已失效,执行删除操作",phoneNumber));
166172
//移除失效账号
@@ -177,13 +183,15 @@ public void check() throws Exception {
177183
}
178184
if (count >= 30){
179185
int number=(int)(Math.random()*(10)+600);
180-
System.out.println("10分钟内打卡数达到30个,解黑休眠"+number+"秒,继续打卡");
186+
System.out.println("10分钟内打卡数达到30个,解黑休眠"+number+"秒,继续检测");
181187
Thread.sleep(number*1000);
182188
count = 0;
183189
}
190+
System.out.println();
184191
flag = 0;
185192
}
186193
}
187194
}
188195

196+
189197
}

0 commit comments

Comments
 (0)