|
@@ -1,12 +1,16 @@
|
|
|
package com.persagy.apm.report.indicator.factory;
|
|
|
|
|
|
+import cn.hutool.core.thread.ExecutorBuilder;
|
|
|
+import com.persagy.apm.common.context.poems.PoemsContext;
|
|
|
import com.persagy.apm.report.dependencies.energyefficiencyweb.model.vo.EnergyEfficiencyDataVO;
|
|
|
import com.persagy.apm.report.detail.model.vo.AttrValueVO;
|
|
|
import junit.framework.TestCase;
|
|
|
import org.assertj.core.util.Lists;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.*;
|
|
|
|
|
|
public class EnergyEfficiencyFactoryTest extends TestCase {
|
|
|
|
|
@@ -33,4 +37,112 @@ public class EnergyEfficiencyFactoryTest extends TestCase {
|
|
|
AttrValueVO maxDays = energyEfficiencyFactory.getMinDays(dailyEnergyEfficiency);
|
|
|
System.out.println(maxDays.getValue());
|
|
|
}
|
|
|
+
|
|
|
+ public void testThreadPool() throws InterruptedException {
|
|
|
+ ExecutorService es = ExecutorBuilder.create()
|
|
|
+ .setCorePoolSize(5)
|
|
|
+ .setMaxPoolSize(10)
|
|
|
+ .setWorkQueue(new LinkedBlockingQueue<>(10))
|
|
|
+ .setHandler(new ThreadPoolExecutor.CallerRunsPolicy())
|
|
|
+ .build();
|
|
|
+ int count = 1000;
|
|
|
+ List<FutureTask<String>> tasks = new ArrayList<>(count);
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ System.out.println("次数" + i);
|
|
|
+ // 添加Future任务,过期时间为5分钟
|
|
|
+ FutureTask<String> future = new FutureTask<>(() -> {
|
|
|
+ System.out.println("开始执行");
|
|
|
+ Thread.sleep(5000);
|
|
|
+ return Thread.currentThread().getId() + "done";
|
|
|
+ });
|
|
|
+ tasks.add(future);
|
|
|
+ }
|
|
|
+ for (FutureTask<String> task : tasks) {
|
|
|
+ es.execute(task);
|
|
|
+ es.execute(() -> {
|
|
|
+ try {
|
|
|
+ System.out.println("获取结果");
|
|
|
+ System.out.println(task.get());
|
|
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
+ System.out.println("发生异常");
|
|
|
+ task.cancel(true);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ Thread.sleep(5000 * count);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testThreadPool1() throws InterruptedException {
|
|
|
+ ExecutorService es = ExecutorBuilder.create()
|
|
|
+ .setCorePoolSize(5)
|
|
|
+ .setMaxPoolSize(10)
|
|
|
+ .setWorkQueue(new LinkedBlockingQueue<>(10))
|
|
|
+ .setHandler(new ThreadPoolExecutor.CallerRunsPolicy())
|
|
|
+ .build();
|
|
|
+ int count = 1000;
|
|
|
+ List<FutureTask<String>> tasks = new ArrayList<>(count);
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ System.out.println("次数" + i);
|
|
|
+ // 添加Future任务,过期时间为5分钟
|
|
|
+ FutureTask<String> task = new FutureTask<>(() -> {
|
|
|
+ System.out.println("开始执行");
|
|
|
+ Thread.sleep(5000);
|
|
|
+ return Thread.currentThread().getId() + "done";
|
|
|
+ });
|
|
|
+ es.execute(task);
|
|
|
+ es.execute(() -> {
|
|
|
+ try {
|
|
|
+ System.out.println("获取结果");
|
|
|
+ System.out.println(task.get());
|
|
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
+ System.out.println("发生异常");
|
|
|
+ task.cancel(true);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ Thread.sleep(5000 * count);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void testThreadPool2() throws InterruptedException {
|
|
|
+ ExecutorService es = ExecutorBuilder.create()
|
|
|
+ .setCorePoolSize(5)
|
|
|
+ .setMaxPoolSize(10)
|
|
|
+ .setWorkQueue(new LinkedBlockingQueue<>(100))
|
|
|
+ .setHandler(new ThreadPoolExecutor.AbortPolicy())
|
|
|
+ .build();
|
|
|
+ ExecutorService es1 = ExecutorBuilder.create()
|
|
|
+ .setCorePoolSize(5)
|
|
|
+ .setMaxPoolSize(10)
|
|
|
+ .setWorkQueue(new LinkedBlockingQueue<>(100))
|
|
|
+ .setHandler(new ThreadPoolExecutor.CallerRunsPolicy())
|
|
|
+ .build();
|
|
|
+ int count = 1000;
|
|
|
+ List<FutureTask<String>> tasks = new ArrayList<>(count);
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ System.out.println("次数" + i);
|
|
|
+ // 添加Future任务,过期时间为5分钟
|
|
|
+ FutureTask<String> task = new FutureTask<>(() -> {
|
|
|
+ System.out.println("开始执行");
|
|
|
+ Thread.sleep(5000);
|
|
|
+ return Thread.currentThread().getId() + "done";
|
|
|
+ });
|
|
|
+ try {
|
|
|
+ es.submit(task);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // es1.execute(() -> {
|
|
|
+// try {
|
|
|
+// System.out.println("获取结果");
|
|
|
+// System.out.println(task.get());
|
|
|
+// } catch (InterruptedException | ExecutionException e) {
|
|
|
+// System.out.println("发生异常");
|
|
|
+// task.cancel(true);
|
|
|
+// }
|
|
|
+// });
|
|
|
+ }
|
|
|
+ Thread.sleep(5000 * count);
|
|
|
+ }
|
|
|
}
|