Browse Source

还是用单线程 多线程会重复插入

yufeng 3 years ago
parent
commit
dbad832086

+ 6 - 5
src/main/java/com/ylcm/sys/controller/AdTheatreController.java

@@ -33,6 +33,7 @@ import javax.annotation.Resource;
33 33
 import javax.servlet.http.HttpServletRequest;
34 34
 import javax.servlet.http.HttpServletResponse;
35 35
 import java.util.ArrayList;
36
+import java.util.Comparator;
36 37
 import java.util.List;
37 38
 import java.util.concurrent.CompletableFuture;
38 39
 
@@ -61,7 +62,7 @@ public class AdTheatreController extends BaseController{
61 62
 	@Resource
62 63
 	private AdTheatreTvStatService adTheatreTvStatService;
63 64
 	@Resource
64
-	private TaskExecutor taskExecutor;
65
+	private TaskExecutor batchExecutor;
65 66
 
66 67
 	/**
67 68
 	 * to 列表页面
@@ -257,11 +258,11 @@ public class AdTheatreController extends BaseController{
257 258
 			// 入库
258 259
 			List<AdTheatre> adTheatres = adTheatreService.batchAdd(list, tvList);
259 260
 
261
+
260 262
             //同步统计数据
261
-			CompletableFuture[] completableFutures = adTheatres.stream().map(x -> CompletableFuture.runAsync(() -> {
262
-				adTheatreTvStatService.saveStatByTheatreTv(x);
263
-			}, taskExecutor)).toArray(CompletableFuture[]::new);
264
-			CompletableFuture.allOf(completableFutures).join();
263
+			for (AdTheatre adTheatre:adTheatres) {
264
+				adTheatreTvStatService.saveStatByTheatreTv(adTheatre);
265
+			}
265 266
 
266 267
 			// 判断执行结果
267 268
 			if (adTheatres != null) {

+ 0 - 6
src/main/java/com/ylcm/sys/service/impl/AdTheatreTvStatServiceImpl.java

@@ -245,10 +245,7 @@ public class AdTheatreTvStatServiceImpl extends ServiceImpl<AdTheatreTvStatMappe
245 245
 		}
246 246
 
247 247
 
248
-		long start = System.currentTimeMillis();
249 248
 		List<AdTheatre> adTheatres = adTheatreMapper.selectList(queryWrapper);
250
-		long end = System.currentTimeMillis();
251
-		log.info("list 耗时, {} ", end - start);
252 249
 
253 250
 		if (CollectionUtils.isEmpty(adTheatres)) {
254 251
 			log.info("AdTheatre数据为空,删掉, {}, {}", queryWrapper.getCustomSqlSegment(), queryWrapper.getParamNameValuePairs());
@@ -302,10 +299,7 @@ public class AdTheatreTvStatServiceImpl extends ServiceImpl<AdTheatreTvStatMappe
302 299
 			adTheatreTvStat.setTvNum(total);
303 300
 			adTheatreTvStat.setTvNumD(total_d);
304 301
 
305
-			start = System.currentTimeMillis();
306 302
 			saveOrUpd(adTheatreTvStat);
307
-			end = System.currentTimeMillis();
308
-			log.info("save 耗时, {} ", end - start);
309 303
 
310 304
 		}
311 305
 	}

+ 16 - 0
src/main/resource/applicationContext-plugin.xml

@@ -26,4 +26,20 @@
26 26
 		</property>
27 27
 	</bean>
28 28
 
29
+
30
+	<bean id="batchExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
31
+		<!-- 线程池维护线程的最少数量 -->
32
+		<property name="corePoolSize" value="2" />
33
+		<!-- 允许的空闲时间 -->
34
+		<property name="keepAliveSeconds" value="200" />
35
+		<!-- 线程池维护线程的最大数量 -->
36
+		<property name="maxPoolSize" value="4" />
37
+		<!-- 缓存队列 -->
38
+		<property name="queueCapacity" value="4" />
39
+		<!-- 对拒绝task的处理策略 -->
40
+		<property name="rejectedExecutionHandler">
41
+			<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/>
42
+		</property>
43
+	</bean>
44
+
29 45
 </beans>