B2B网络软件
标题:
AIWROK软件演示多线程创建暂停恢复
[打印本页]
作者:
YYPOST群发软件
时间:
4 小时前
标题:
AIWROK软件演示多线程创建暂停恢复
AIWROK软件演示多线程创建暂停恢复
2.png
(1.24 MB, 下载次数: 0)
下载附件
4 小时前
上传
/*
AIWROK多线程综合操作示例
适用环境:安卓Rhino JavaScript引擎(ES5)
功能:综合演示多线程创建、暂停/恢复、优先级控制、许可证同步等核心功能
场景模拟:一个自动化任务系统
- 主监控线程(高优先级):监控系统状态
- 数据处理线程(中优先级):处理业务数据
- 日志记录线程(低优先级):记录操作日志
- 使用许可证确保关键操作的原子性
*/
// ==================== 全局配置 ====================
var MIN_PRIORITY = 1; // 最低优先级
var NORM_PRIORITY = 5; // 正常优先级
var MAX_PRIORITY = 10; // 最高优先级
// ==================== 日志悬浮窗初始化 ====================
var logWindow = new floatUI();
var logText = null;
function initLogWindow() {
try {
logWindow.loadXML(
'<LinearLayout orientation="vertical" w="match_parent" h="match_parent" gravity="left">' +
' <TextView id="logText" textColor="#00ff00" background="#000000" textSize="9" layout_width="wrap_content" layout_height="wrap_content" />' +
'</LinearLayout>'
);
logText = logWindow.findViewById('logText');
if (logText) {
setTimeout(function() {
logWindow.setPosition(10, 100);
}, 100);
logText.setText("AIWROK多线程综合示例\n");
} else {
print.log("日志视图未正确初始化");
}
} catch (e) {
print.log("创建日志悬浮窗失败: " + String(e));
}
}
// 更新日志显示
function updateLog(content) {
print.log(content);
try {
if (logText) {
var currentTime = new Date().toLocaleTimeString();
var logContent = logText.getText() || "";
var newLogContent = "[" + currentTime + "] " + content + "\n" + logContent;
// 限制日志行数,避免内容过多(增加到100行以填满全屏)
var logLines = newLogContent.split("\n");
if (logLines.length > 100) {
newLogContent = logLines.slice(0, 100).join("\n");
}
logText.setText(newLogContent);
}
} catch (e) {
print.log("更新日志失败: " + String(e));
}
}
// ==================== 线程许可证工具 ====================
function ThreadLicense() {
var _isLocked = false;
var _waitingQueue = [];
this.acquire = function(callback) {
if (typeof callback !== 'function') {
return false;
}
if (!_isLocked) {
_isLocked = true;
try {
callback(this.release.bind(this));
} catch (e) {
this.release();
throw e;
}
return true;
} else {
_waitingQueue.push(callback);
return false;
}
};
this.release = function() {
_isLocked = false;
if (_waitingQueue.length > 0) {
var nextCallback = _waitingQueue.shift();
this.acquire(nextCallback);
}
};
this.isLocked = function() {
return _isLocked;
};
this.getQueueLength = function() {
return _waitingQueue.length;
};
}
// 创建全局许可证用于数据库操作
var dbLicense = new ThreadLicense();
// ==================== 可暂停线程类 ====================
function PausableThread(name, priority) {
var _thread = new thread();
var _isPaused = false;
var _isRunning = false;
var _name = name || "未命名线程";
var _priority = priority || NORM_PRIORITY;
this.pause = function() {
_isPaused = true;
updateLog("[" + _name + "] 线程已暂停");
};
this.resume = function() {
if (_isPaused) {
_isPaused = false;
updateLog("[" + _name + "] 线程已恢复");
}
};
this.isPaused = function() {
return _isPaused;
};
this.isAlive = function() {
return _thread.isAlive();
};
this.stop = function() {
_isRunning = false;
_thread.stop();
updateLog("[" + _name + "] 线程已停止");
};
var _checkPause = function() {
while (_isPaused && _isRunning) {
try {
java.lang.Thread.sleep(100);
} catch (e) {
updateLog("[" + _name + "] 暂停等待异常: " + String(e));
}
}
};
this.start = function(taskFunction) {
_isRunning = true;
_isPaused = false;
var threadName = _name;
var priority = _priority;
_thread.runJsCode(function() {
try {
// 设置线程优先级
java.lang.Thread.currentThread().setPriority(priority);
var checkPause = _checkPause;
var isRunning = function() { return _isRunning; };
taskFunction(checkPause, isRunning, threadName);
} catch (e) {
updateLog("[" + threadName + "] 线程执行异常: " + String(e));
}
}, _name);
updateLog("[" + _name + "] 线程已启动 (优先级:" + priority + ")");
};
this.getName = function() {
return _name;
};
}
// ==================== 业务线程定义 ====================
// 1. 主监控线程(高优先级)- 获取百度首页并监控状态
function monitorTask(checkPause, isRunning, threadName) {
updateLog("[" + threadName + "] 开始获取百度首页");
var cycleCount = 0;
while (isRunning() && cycleCount < 5) {
checkPause();
cycleCount++;
try {
// 使用okHttp.get获取百度首页
var httpObj = new okHttp();
var response = httpObj.get("https://www.baidu.com");
if (response) {
// 截取前100个字符显示
var preview = String(response).substring(0, 100);
updateLog("[" + threadName + "] 第" + cycleCount + "次请求: 成功");
updateLog("[" + threadName + "] 响应预览: " + preview + "...");
} else {
updateLog("[" + threadName + "] 第" + cycleCount + "次请求: 失败");
}
} catch (e) {
updateLog("[" + threadName + "] 第" + cycleCount + "次请求异常: " + String(e));
}
try {
java.lang.Thread.sleep(1500);
} catch (e) {
updateLog("[" + threadName + "] 休眠异常: " + String(e));
}
}
updateLog("[" + threadName + "] 监控任务完成");
}
// 2. 数据处理线程(中优先级)- 获取百度首页数据
function dataProcessTask(checkPause, isRunning, threadName) {
updateLog("[" + threadName + "] 开始处理百度数据");
var processedCount = 0;
while (isRunning() && processedCount < 5) {
checkPause();
processedCount++;
// 使用许可证确保操作的原子性
dbLicense.acquire(function(release) {
updateLog("[" + threadName + "] 获取数据库许可证,处理第" + processedCount + "条数据");
try {
// 获取百度首页
var httpObj = new okHttp();
var response = httpObj.get("https://www.baidu.com");
if (response) {
// 截取前80个字符显示
var preview = String(response).substring(0, 80);
updateLog("[" + threadName + "] 第" + processedCount + "条数据获取成功");
updateLog("[" + threadName + "] 响应: " + preview + "...");
} else {
updateLog("[" + threadName + "] 第" + processedCount + "条数据获取失败");
}
} catch (e) {
updateLog("[" + threadName + "] 第" + processedCount + "条数据异常: " + String(e));
}
updateLog("[" + threadName + "] 第" + processedCount + "条数据处理完成");
release();
});
try {
java.lang.Thread.sleep(1000);
} catch (e) {
updateLog("[" + threadName + "] 休眠异常: " + String(e));
}
}
updateLog("[" + threadName + "] 数据处理完成,共处理" + processedCount + "条");
}
// 3. 日志记录线程(低优先级)- 获取百度首页并记录
function logRecordTask(checkPause, isRunning, threadName) {
updateLog("[" + threadName + "] 开始记录百度访问日志");
var logCount = 0;
while (isRunning() && logCount < 10) {
checkPause();
logCount++;
try {
// 获取百度首页
var httpObj = new okHttp();
var response = httpObj.get("https://www.baidu.com");
if (response) {
// 截取前60个字符显示
var preview = String(response).substring(0, 60);
updateLog("[" + threadName + "] 记录访问 #" + logCount + " - 成功");
updateLog("[" + threadName + "] 响应: " + preview + "...");
} else {
updateLog("[" + threadName + "] 记录访问 #" + logCount + " - 失败");
}
} catch (e) {
updateLog("[" + threadName + "] 记录访问 #" + logCount + " 异常");
}
try {
java.lang.Thread.sleep(800);
} catch (e) {
updateLog("[" + threadName + "] 休眠异常: " + String(e));
}
}
updateLog("[" + threadName + "] 日志记录完成,共记录" + logCount + "条");
}
// ==================== 线程管理 ====================
// 创建三个不同优先级的线程
var monitorThread = new PausableThread("监控线程", MAX_PRIORITY);
var dataThread = new PausableThread("数据处理线程", NORM_PRIORITY);
var logThread = new PausableThread("日志线程", MIN_PRIORITY);
// 启动所有线程
function startAllThreads() {
updateLog("========== 启动所有线程 ==========");
monitorThread.start(monitorTask);
sleep.second(秒=0.2);
dataThread.start(dataProcessTask);
sleep.second(秒=0.2);
logThread.start(logRecordTask);
updateLog("所有线程已启动");
}
// 暂停所有线程
function pauseAllThreads() {
updateLog("========== 暂停所有线程 ==========");
monitorThread.pause();
dataThread.pause();
logThread.pause();
}
// 恢复所有线程
function resumeAllThreads() {
updateLog("========== 恢复所有线程 ==========");
monitorThread.resume();
dataThread.resume();
logThread.resume();
}
// 停止所有线程
function stopAllThreads() {
updateLog("========== 停止所有线程 ==========");
monitorThread.stop();
dataThread.stop();
logThread.stop();
}
// 监控线程状态
function monitorThreadStatus() {
var isMonitoring = true;
function checkStatus() {
if (!isMonitoring) return;
try {
var mStatus = monitorThread.isAlive() ? "运行" : "停止";
var dStatus = dataThread.isAlive() ? "运行" : "停止";
var lStatus = logThread.isAlive() ? "运行" : "停止";
var mPause = monitorThread.isPaused() ? "[暂停]" : "";
var dPause = dataThread.isPaused() ? "[暂停]" : "";
var lPause = logThread.isPaused() ? "[暂停]" : "";
updateLog("状态 | 监控:" + mStatus + mPause + " | 数据:" + dStatus + dPause + " | 日志:" + lStatus + lPause);
// 如果所有线程都已结束,停止监控
if (!monitorThread.isAlive() && !dataThread.isAlive() && !logThread.isAlive()) {
isMonitoring = false;
updateLog("所有线程已结束");
return;
}
// 继续下一次检查
setTimeout(checkStatus, 3000);
} catch (e) {
updateLog("监控线程状态出错: " + String(e));
}
}
// 启动第一次检查
setTimeout(checkStatus, 3000);
}
// ==================== 自动演示流程 ====================
function autoDemo() {
updateLog("========== 开始自动演示 ==========");
updateLog("演示场景: 多线程并发访问 https://www.baidu.com");
updateLog("线程1(高优先级): GET 百度首页 - 5次");
updateLog("线程2(中优先级): GET 百度首页 - 5次 (带许可证)");
updateLog("线程3(低优先级): GET 百度首页 - 10次");
updateLog("===============================");
updateLog("演示流程:");
updateLog("1. 启动3个不同优先级的线程");
updateLog("2. 3秒后暂停所有线程");
updateLog("3. 5秒后恢复所有线程");
updateLog("4. 8秒后再次暂停");
updateLog("5. 10秒后恢复");
updateLog("6. 15秒后停止所有线程");
updateLog("===============================");
// 启动所有线程
startAllThreads();
// 3秒后暂停
setTimeout(function() {
pauseAllThreads();
}, 3000);
// 5秒后恢复
setTimeout(function() {
resumeAllThreads();
}, 5000);
// 8秒后再次暂停
setTimeout(function() {
pauseAllThreads();
}, 8000);
// 10秒后恢复
setTimeout(function() {
resumeAllThreads();
}, 10000);
// 15秒后停止
setTimeout(function() {
stopAllThreads();
}, 15000);
}
// ==================== 程序入口 ====================
initLogWindow();
updateLog("AIWROK多线程综合操作示例");
updateLog("版本: 1.0");
updateLog("");
// 延迟2秒后开始自动演示
setTimeout(function() {
autoDemo();
monitorThreadStatus();
}, 2000);
复制代码
欢迎光临 B2B网络软件 (http://bbs.niubt.cn/)
Powered by Discuz! X3.2