|
|
 | |  |  | AIWROK软件创建可暂停恢复的多线程任务
- /*
- 🍎交流QQ群711841924群一,苹果内测群,528816639
- 适用环境:安卓Rhino JavaScript引擎(ES5)
- 功能:演示如何创建可暂停、恢复的多线程任务
- */
- // 创建日志悬浮窗
- var logWindow = new floatUI();
- var logText = null;
- // 初始化日志悬浮窗
- function initLogWindow() {
- try {
- logWindow.loadXML(
- '<LinearLayout orientation="vertical" w="350" h="250" 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("多线程暂停示例开始运行...");
- } else {
- printl("日志视图未正确初始化");
- }
- } catch (e) {
- printl("创建日志悬浮窗失败: " + String(e));
- }
- }
- // 更新日志显示内容
- function updateLog(content) {
- printl(content);
-
- try {
- if (logText) {
- var currentTime = new Date().toLocaleTimeString();
- var logContent = logText.getText() || "";
- var newLogContent = "[" + currentTime + "] " + content + "\n" + logContent;
- var logLines = newLogContent.split("\n");
- if (logLines.length > 25) {
- newLogContent = logLines.slice(0, 25).join("\n");
- }
- logText.setText(newLogContent);
- }
- } catch (e) {
- printl("更新日志显示失败: " + String(e));
- }
- }
- // 可暂停线程类
- function PausableThread(name) {
- var _thread = new thread();
- var _isPaused = false;
- var _isRunning = false;
- var _name = name || "未命名线程";
-
- // 暂停线程
- 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;
-
- _thread.runJsCode(function() {
- var task = taskFunction;
- var checkPause = _checkPause;
- var isRunning = function() { return _isRunning; };
- var threadName = _name;
-
- try {
- task(checkPause, isRunning, threadName);
- } catch (e) {
- updateLog("[" + threadName + "] 线程执行异常: " + String(e));
- }
- }, _name);
-
- updateLog("[" + _name + "] 线程已启动");
- };
-
- // 获取线程名称
- this.getName = function() {
- return _name;
- };
- }
- // 创建多个可暂停线程
- var thread1 = new PausableThread("工作线程1");
- var thread2 = new PausableThread("工作线程2");
- var thread3 = new PausableThread("工作线程3");
- // 线程任务函数
- function workerTask(checkPause, isRunning, threadName) {
- var counter = 0;
-
- while (isRunning() && counter < 20) {
- // 检查暂停状态
- checkPause();
-
- counter++;
- updateLog("[" + threadName + "] 执行任务 " + counter + "/20");
-
- try {
- java.lang.Thread.sleep(800);
- } catch (e) {
- updateLog("[" + threadName + "] 休眠异常: " + String(e));
- }
- }
-
- updateLog("[" + threadName + "] 任务完成");
- }
- // 启动所有线程
- function startAllThreads() {
- updateLog("=== 启动所有线程 ===");
- thread1.start(workerTask);
- java.lang.Thread.sleep(200);
- thread2.start(workerTask);
- java.lang.Thread.sleep(200);
- thread3.start(workerTask);
- }
- // 暂停所有线程
- function pauseAllThreads() {
- updateLog("=== 暂停所有线程 ===");
- thread1.pause();
- thread2.pause();
- thread3.pause();
- }
- // 恢复所有线程
- function resumeAllThreads() {
- updateLog("=== 恢复所有线程 ===");
- thread1.resume();
- thread2.resume();
- thread3.resume();
- }
- // 停止所有线程
- function stopAllThreads() {
- updateLog("=== 停止所有线程 ===");
- thread1.stop();
- thread2.stop();
- thread3.stop();
- }
- // 监控线程状态
- function monitorThreads() {
- var monitorInterval = setInterval(function() {
- var status1 = thread1.isAlive() ? "运行中" : "已停止";
- var status2 = thread2.isAlive() ? "运行中" : "已停止";
- var status3 = thread3.isAlive() ? "运行中" : "已停止";
-
- var pause1 = thread1.isPaused() ? "[暂停]" : "";
- var pause2 = thread2.isPaused() ? "[暂停]" : "";
- var pause3 = thread3.isPaused() ? "[暂停]" : "";
-
- updateLog("线程状态: " + status1 + pause1 + " | " + status2 + pause2 + " | " + status3 + pause3);
-
- if (!thread1.isAlive() && !thread2.isAlive() && !thread3.isAlive()) {
- clearInterval(monitorInterval);
- updateLog("所有线程已停止");
- }
- }, 2000);
- }
- // 自动演示流程
- function autoDemo() {
- updateLog("=== 开始自动演示 ===");
-
- // 启动线程
- startAllThreads();
-
- // 5秒后暂停所有线程
- setTimeout(function() {
- pauseAllThreads();
- }, 5000);
-
- // 10秒后恢复所有线程
- setTimeout(function() {
- resumeAllThreads();
- }, 10000);
-
- // 15秒后再次暂停
- setTimeout(function() {
- pauseAllThreads();
- }, 15000);
-
- // 20秒后恢复
- setTimeout(function() {
- resumeAllThreads();
- }, 20000);
-
- // 30秒后停止所有线程
- setTimeout(function() {
- stopAllThreads();
- }, 30000);
- }
- // 手动控制演示
- function manualDemo() {
- updateLog("=== 手动控制模式 ===");
- updateLog("使用以下方法控制线程:");
- updateLog("startAllThreads() - 启动所有线程");
- updateLog("pauseAllThreads() - 暂停所有线程");
- updateLog("resumeAllThreads() - 恢复所有线程");
- updateLog("stopAllThreads() - 停止所有线程");
- updateLog("thread1.pause() - 暂停线程1");
- updateLog("thread1.resume() - 恢复线程1");
- }
- // 初始化并运行
- initLogWindow();
- updateLog("AIWROK多线程暂停示例");
- updateLog("选择演示模式:");
- updateLog("1. 自动演示: autoDemo()");
- updateLog("2. 手动控制: manualDemo()");
- // 默认运行自动演示
- setTimeout(function() {
- autoDemo();
- monitorThreads();
- }, 2000);
复制代码
| |  | |  |
|
untoAIWROK软件类型转换方法例子nextnocontent
|