自动发帖软件

标题: AIWROK软件创建可暂停恢复的多线程任务 [打印本页]

作者: 发帖软件    时间: 4 小时前
标题: AIWROK软件创建可暂停恢复的多线程任务
AIWROK软件创建可暂停恢复的多线程任务
AIWROK软件创建可暂停恢复的多线程任务 群发软件发帖工具
AIWROK软件创建可暂停恢复的多线程任务 群发软件发帖工具
AIWROK软件创建可暂停恢复的多线程任务 群发软件发帖工具
  1. /*
  2.   🍎交流QQ群711841924群一,苹果内测群,528816639
  3.    适用环境:安卓Rhino JavaScript引擎(ES5)
  4.    功能:演示如何创建可暂停、恢复的多线程任务
  5. */

  6. // 创建日志悬浮窗
  7. var logWindow = new floatUI();
  8. var logText = null;

  9. // 初始化日志悬浮窗
  10. function initLogWindow() {
  11.     try {
  12.         logWindow.loadXML(
  13.             '<LinearLayout orientation="vertical" w="350" h="250" gravity="left">' +
  14.             '  <TextView id="logText" textColor="#00ff00" background="#000000" textSize="9" layout_width="wrap_content" layout_height="wrap_content" />' +
  15.             '</LinearLayout>'
  16.         );
  17.         
  18.         logText = logWindow.findViewById('logText');
  19.         
  20.         if (logText) {
  21.             setTimeout(function() {
  22.                 logWindow.setPosition(10, 100);
  23.             }, 100);
  24.             
  25.             logText.setText("多线程暂停示例开始运行...");
  26.         } else {
  27.             printl("日志视图未正确初始化");
  28.         }
  29.     } catch (e) {
  30.         printl("创建日志悬浮窗失败: " + String(e));
  31.     }
  32. }

  33. // 更新日志显示内容
  34. function updateLog(content) {
  35.     printl(content);
  36.    
  37.     try {
  38.         if (logText) {
  39.             var currentTime = new Date().toLocaleTimeString();
  40.             var logContent = logText.getText() || "";
  41.             var newLogContent = "[" + currentTime + "] " + content + "\n" + logContent;
  42.             var logLines = newLogContent.split("\n");
  43.             if (logLines.length > 25) {
  44.                 newLogContent = logLines.slice(0, 25).join("\n");
  45.             }
  46.             logText.setText(newLogContent);
  47.         }
  48.     } catch (e) {
  49.         printl("更新日志显示失败: " + String(e));
  50.     }
  51. }

  52. // 可暂停线程类
  53. function PausableThread(name) {
  54.     var _thread = new thread();
  55.     var _isPaused = false;
  56.     var _isRunning = false;
  57.     var _name = name || "未命名线程";
  58.    
  59.     // 暂停线程
  60.     this.pause = function() {
  61.         _isPaused = true;
  62.         updateLog("[" + _name + "] 线程已暂停");
  63.     };
  64.    
  65.     // 恢复线程
  66.     this.resume = function() {
  67.         if (_isPaused) {
  68.             _isPaused = false;
  69.             updateLog("[" + _name + "] 线程已恢复");
  70.         }
  71.     };
  72.    
  73.     // 检查是否暂停
  74.     this.isPaused = function() {
  75.         return _isPaused;
  76.     };
  77.    
  78.     // 检查是否运行中
  79.     this.isAlive = function() {
  80.         return _thread.isAlive();
  81.     };
  82.    
  83.     // 停止线程
  84.     this.stop = function() {
  85.         _isRunning = false;
  86.         _thread.stop();
  87.         updateLog("[" + _name + "] 线程已停止");
  88.     };
  89.    
  90.     // 内部暂停检查方法
  91.     var _checkPause = function() {
  92.         while (_isPaused && _isRunning) {
  93.             try {
  94.                 java.lang.Thread.sleep(100);
  95.             } catch (e) {
  96.                 updateLog("[" + _name + "] 暂停等待异常: " + String(e));
  97.             }
  98.         }
  99.     };
  100.    
  101.     // 启动线程
  102.     this.start = function(taskFunction) {
  103.         _isRunning = true;
  104.         _isPaused = false;
  105.         
  106.         _thread.runJsCode(function() {
  107.             var task = taskFunction;
  108.             var checkPause = _checkPause;
  109.             var isRunning = function() { return _isRunning; };
  110.             var threadName = _name;
  111.             
  112.             try {
  113.                 task(checkPause, isRunning, threadName);
  114.             } catch (e) {
  115.                 updateLog("[" + threadName + "] 线程执行异常: " + String(e));
  116.             }
  117.         }, _name);
  118.         
  119.         updateLog("[" + _name + "] 线程已启动");
  120.     };
  121.    
  122.     // 获取线程名称
  123.     this.getName = function() {
  124.         return _name;
  125.     };
  126. }

  127. // 创建多个可暂停线程
  128. var thread1 = new PausableThread("工作线程1");
  129. var thread2 = new PausableThread("工作线程2");
  130. var thread3 = new PausableThread("工作线程3");

  131. // 线程任务函数
  132. function workerTask(checkPause, isRunning, threadName) {
  133.     var counter = 0;
  134.    
  135.     while (isRunning() && counter < 20) {
  136.         // 检查暂停状态
  137.         checkPause();
  138.         
  139.         counter++;
  140.         updateLog("[" + threadName + "] 执行任务 " + counter + "/20");
  141.         
  142.         try {
  143.             java.lang.Thread.sleep(800);
  144.         } catch (e) {
  145.             updateLog("[" + threadName + "] 休眠异常: " + String(e));
  146.         }
  147.     }
  148.    
  149.     updateLog("[" + threadName + "] 任务完成");
  150. }

  151. // 启动所有线程
  152. function startAllThreads() {
  153.     updateLog("=== 启动所有线程 ===");
  154.     thread1.start(workerTask);
  155.     java.lang.Thread.sleep(200);
  156.     thread2.start(workerTask);
  157.     java.lang.Thread.sleep(200);
  158.     thread3.start(workerTask);
  159. }

  160. // 暂停所有线程
  161. function pauseAllThreads() {
  162.     updateLog("=== 暂停所有线程 ===");
  163.     thread1.pause();
  164.     thread2.pause();
  165.     thread3.pause();
  166. }

  167. // 恢复所有线程
  168. function resumeAllThreads() {
  169.     updateLog("=== 恢复所有线程 ===");
  170.     thread1.resume();
  171.     thread2.resume();
  172.     thread3.resume();
  173. }

  174. // 停止所有线程
  175. function stopAllThreads() {
  176.     updateLog("=== 停止所有线程 ===");
  177.     thread1.stop();
  178.     thread2.stop();
  179.     thread3.stop();
  180. }

  181. // 监控线程状态
  182. function monitorThreads() {
  183.     var monitorInterval = setInterval(function() {
  184.         var status1 = thread1.isAlive() ? "运行中" : "已停止";
  185.         var status2 = thread2.isAlive() ? "运行中" : "已停止";
  186.         var status3 = thread3.isAlive() ? "运行中" : "已停止";
  187.         
  188.         var pause1 = thread1.isPaused() ? "[暂停]" : "";
  189.         var pause2 = thread2.isPaused() ? "[暂停]" : "";
  190.         var pause3 = thread3.isPaused() ? "[暂停]" : "";
  191.         
  192.         updateLog("线程状态: " + status1 + pause1 + " | " + status2 + pause2 + " | " + status3 + pause3);
  193.         
  194.         if (!thread1.isAlive() && !thread2.isAlive() && !thread3.isAlive()) {
  195.             clearInterval(monitorInterval);
  196.             updateLog("所有线程已停止");
  197.         }
  198.     }, 2000);
  199. }

  200. // 自动演示流程
  201. function autoDemo() {
  202.     updateLog("=== 开始自动演示 ===");
  203.    
  204.     // 启动线程
  205.     startAllThreads();
  206.    
  207.     // 5秒后暂停所有线程
  208.     setTimeout(function() {
  209.         pauseAllThreads();
  210.     }, 5000);
  211.    
  212.     // 10秒后恢复所有线程
  213.     setTimeout(function() {
  214.         resumeAllThreads();
  215.     }, 10000);
  216.    
  217.     // 15秒后再次暂停
  218.     setTimeout(function() {
  219.         pauseAllThreads();
  220.     }, 15000);
  221.    
  222.     // 20秒后恢复
  223.     setTimeout(function() {
  224.         resumeAllThreads();
  225.     }, 20000);
  226.    
  227.     // 30秒后停止所有线程
  228.     setTimeout(function() {
  229.         stopAllThreads();
  230.     }, 30000);
  231. }

  232. // 手动控制演示
  233. function manualDemo() {
  234.     updateLog("=== 手动控制模式 ===");
  235.     updateLog("使用以下方法控制线程:");
  236.     updateLog("startAllThreads() - 启动所有线程");
  237.     updateLog("pauseAllThreads() - 暂停所有线程");
  238.     updateLog("resumeAllThreads() - 恢复所有线程");
  239.     updateLog("stopAllThreads() - 停止所有线程");
  240.     updateLog("thread1.pause() - 暂停线程1");
  241.     updateLog("thread1.resume() - 恢复线程1");
  242. }

  243. // 初始化并运行
  244. initLogWindow();
  245. updateLog("AIWROK多线程暂停示例");
  246. updateLog("选择演示模式:");
  247. updateLog("1. 自动演示: autoDemo()");
  248. updateLog("2. 手动控制: manualDemo()");

  249. // 默认运行自动演示
  250. setTimeout(function() {
  251.     autoDemo();
  252.     monitorThreads();
  253. }, 2000);
复制代码







欢迎光临 自动发帖软件 (http://www.fatiegongju.com/) Powered by Discuz! X3.2