我这结束后还是会继续输出1,我需要让它停止的时候就不执行这段代码块的内容了
AtomicReference<String> result = new AtomicReference<>("");
CountDownLatch latch = new CountDownLatch(1);
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
Runnable task = () -> {
// 在这里编写发送请求的代码
if (StringUtils.isEmpty(signDTO.getCookie())) {
return;
}
String signList = getSignList(signDTO.getCookie());
Gson gson = new Gson();
SignList signList1 = gson.fromJson(signList, SignList.class);
SignListData[] signListData = signList1.getData();
int signListLengths = signListData.length;
if (signListLengths != signListLength) {
for (SignListData signListData2 : signListData) {
// 未签到
if (signListData2.getType() != 1) {
Long start = signListData2.getStart();
Date date = new Date();
long time = date.getTime();
Sign sign = new Sign(signDTO.getCountry(), signDTO.getProvince(), signDTO.getCity(), signDTO.getDistrict(), signDTO.getStreet(), signDTO.getLatitude(), signDTO.getLongitude());
String response = "";
Response response1 = null;
// 找到今天的数据
if (start >= time) {
// 开始签到
response = sendSign(sign, signDTO.getCookie(), signListData2.getId(), signListData2.getSchoolId(), signListData2.getSignId());
result.set(response);
response1 = gson.fromJson(response, Response.class);
} else {
response = sendSign(sign, signDTO.getCookie(), signListData2.getId(), signListData2.getSchoolId(), signListData2.getSignId());
result.set(response);
response1 = gson.fromJson(response, Response.class);
}
sendMessage("我在校园自动签到助手提醒您", response1.getMessage(), signListData2.getSignTitle(), signListData2.getSignContext(), signDTO.getUid());
}
}
}
signListLength = signListLengths;
latch.countDown();
};
// 设置任务开始的延迟时间(以毫秒为单位,这里为0表示立即执行)
long initialDelay = 0;
// 设置任务的执行间隔时间(以毫秒为单位,这里为1小时)
long interval = 10000;
executor.scheduleAtFixedRate(task, initialDelay, interval, TimeUnit.MILLISECONDS);
latch.await();
System.out.println(result.get());
return result.get();
@ada; 请帮忙解答
你可以使用 ScheduledExecutorService 来创建定时任务,并且通过调用 shutdown() 方法来停止正在执行的任务。
思路是这样你看下,如有帮助给个采纳谢谢
Promise 虽然解决了回调地狱的问题,但是代码看起来仍然不简洁。
使用异步函数简化代码提高异步编程体验。
异步函数概述
const fs = require("fs")
function readFile(path) {
return new Promise(function (resolve, reject) {
fs.readFile(path, "utf-8", function (error, data) {
if (error) return reject(error)
resolve(data)
})
})
}
async function getFileContent() {
let x = await readFile("./x.txt")
let y = await readFile("./y.txt")
let z = await readFile("./z.txt")
return [x, y, z]
}
getFileContent().then(console.log)
async 声明异步函数的关键字,异步函数的返回值会被自动填充到 Promise 对象中。
await 关键字后面只能放置返回 Promise 对象的 API。
await 关键字可以暂停函数执行,等待 Promise 执行完后返回执行结果。
await 关键字只能出现在异步函数中。
util.promisify
在 Node.js 平台下,所有异步方法使用的都是基于回调函数的异步编程。为了使用异步函数提高异步编程体验,可以使用 util 模块下面的 promisify 方法将基于回调函数的异步 API 转换成返回 Promise 的API。
const fs = require("fs")
const util = require("util")
const readFile = util.promisify(fs.readFile)
async function getFileContent() {
let x = await readFile("./x.txt", "utf-8")
let y = await readFile("./y.txt", "utf-8")
let z = await readFile("./z.txt", "utf-8")
return [x, y, z]
}
getFileContent().then(console.log)