diff --git a/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java b/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java index 8544e3f..aece88d 100644 --- a/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java +++ b/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java @@ -56,6 +56,7 @@ public class TaskImpl implements ITask { Outlook next = iterator.next(); /*根据用户设置生成随机数*/ String Expiration = getRandom(next.getCronTimeRandomStart(), next.getCronTimeRandomEnd()); + System.out.println("生成随机调用时间,github ID" + next.getGithubId() + ",时间:" + Expiration); send(next.getGithubId(), Expiration); } } diff --git a/src/test/java/io/qyi/e5/string/MyThread.java b/src/test/java/io/qyi/e5/string/MyThread.java new file mode 100644 index 0000000..386f2fa --- /dev/null +++ b/src/test/java/io/qyi/e5/string/MyThread.java @@ -0,0 +1,46 @@ +package io.qyi.e5.string; + +import java.util.Date; +import java.util.Random; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * @program: e5 + * @description: + * @author: 落叶随风 + * @create: 2020-04-20 00:38 + **/ +public class MyThread implements Runnable { + private Random random = null; + + public MyThread() { + random = new Random(); + } + + @Override + public void run() { + try { + System.out.println("任务执行开始:" + new Date()); + /**使用随机延时[0-3]秒来模拟执行任务*/ + int sleepNumber = random.nextInt(3); + TimeUnit.SECONDS.sleep(2); + System.out.println("任务执行完毕:" + new Date()); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + ScheduledThreadPoolExecutor service = new ScheduledThreadPoolExecutor(2); + System.out.println("开始任务"); + + for (int i = 0; i < 4; i++) { + //延时3秒执行 + service.schedule(new MyThread(), 3, TimeUnit.SECONDS); + } + System.out.println("结束任务"); + } +} diff --git a/src/test/java/io/qyi/e5/string/dome01.java b/src/test/java/io/qyi/e5/string/dome01.java index be53f9a..f941365 100644 --- a/src/test/java/io/qyi/e5/string/dome01.java +++ b/src/test/java/io/qyi/e5/string/dome01.java @@ -4,10 +4,15 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import lombok.SneakyThrows; import org.junit.jupiter.api.Test; +import java.util.Date; import java.util.Iterator; import java.util.Random; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * @program: e5 @@ -44,9 +49,15 @@ public class dome01 { for (int i = 0; i < 30; i++) { System.out.println(getRandom(3600,7200)); } + } + + @Test + public void ScheduledExecutor(){ } + + public String getRandom(int start, int end){ Random r = new Random(); String Expiration = String.valueOf((r.nextInt(end-start +1) + start) );