5
5
import com .alibaba .fastjson .JSONObject ;
6
6
import com .wmg .Service .ExecService ;
7
7
import com .wmg .util .TimeUtil ;
8
+ import lombok .SneakyThrows ;
8
9
import lombok .extern .slf4j .Slf4j ;
9
10
import org .springframework .beans .factory .annotation .Autowired ;
10
11
import org .springframework .beans .factory .annotation .Value ;
18
19
import java .util .Date ;
19
20
import java .util .Objects ;
20
21
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 ;
24
24
25
25
/**
26
26
* Description: 多线程执行定时任务 官网:www.fhadmin.org
35
35
//所有的定时任务都放在一个线程池中,定时任务启动时使用不同都线程。
36
36
public class Task implements SchedulingConfigurer {
37
37
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 ;
40
44
41
45
//定时表达式
42
46
@ Value ("${demo.corn}" )
@@ -74,45 +78,46 @@ public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
74
78
*@date: 2022/6/9 1:06
75
79
*/
76
80
public void process () throws InterruptedException {
77
- //将计数器归零
78
81
count = 0 ;
79
82
Set <String > keys = redisTemplate .keys ("XiaoMiYunDong_*" );
83
+
80
84
if (!Objects .isNull (keys )) {
81
85
for (String str : keys ) {
86
+ count ++;
82
87
//final int j=i; //关键是这一句代码,将 i 转化为 j,这样j 还是final类型的参与线程
83
88
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 ());
98
98
// 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
+ }
105
110
}
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 +"秒后继续打卡" );
112
115
Thread .sleep (number *1000 );
113
116
count = 0 ;
117
+ // atomicInteger.set(0);
114
118
}
115
119
}
120
+
116
121
}
117
122
118
123
}
@@ -137,9 +142,9 @@ public void check() throws Exception {
137
142
String password = "" ;
138
143
Integer minSteps = 1 ;
139
144
Integer maxSteps = 100000 ;
145
+ Object redisData = redisTemplate .opsForValue ().get (key );
140
146
for (int i = 0 ; i < 3 ; i ++){
141
147
try {
142
- Object redisData = redisTemplate .opsForValue ().get (key );
143
148
if (!Objects .isNull (redisData )){
144
149
count ++;
145
150
JSONObject responseJo = JSONObject .parseObject (redisData .toString ());
@@ -157,10 +162,11 @@ public void check() throws Exception {
157
162
flag ++;
158
163
}
159
164
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);
163
168
}
169
+
164
170
if (flag == 3 ){
165
171
System .out .println (StrUtil .format ("当前账号:{},已失效,执行删除操作" ,phoneNumber ));
166
172
//移除失效账号
@@ -177,13 +183,15 @@ public void check() throws Exception {
177
183
}
178
184
if (count >= 30 ){
179
185
int number =(int )(Math .random ()*(10 )+600 );
180
- System .out .println ("10分钟内打卡数达到30个,解黑休眠" +number +"秒,继续打卡 " );
186
+ System .out .println ("10分钟内打卡数达到30个,解黑休眠" +number +"秒,继续检测 " );
181
187
Thread .sleep (number *1000 );
182
188
count = 0 ;
183
189
}
190
+ System .out .println ();
184
191
flag = 0 ;
185
192
}
186
193
}
187
194
}
188
195
196
+
189
197
}
0 commit comments