B2B网络软件

标题: AIWROK软件演示多线程创建暂停恢复 [打印本页]

作者: YYPOST群发软件    时间: 4 小时前
标题: AIWROK软件演示多线程创建暂停恢复

AIWROK软件演示多线程创建暂停恢复
AIWROK软件演示多线程创建暂停恢复 B2B网络软件

  1. /*
  2.    AIWROK多线程综合操作示例
  3.    适用环境:安卓Rhino JavaScript引擎(ES5)
  4.    功能:综合演示多线程创建、暂停/恢复、优先级控制、许可证同步等核心功能
  5.    
  6.    场景模拟:一个自动化任务系统
  7.    - 主监控线程(高优先级):监控系统状态
  8.    - 数据处理线程(中优先级):处理业务数据
  9.    - 日志记录线程(低优先级):记录操作日志
  10.    - 使用许可证确保关键操作的原子性
  11. */

  12. // ==================== 全局配置 ====================
  13. var MIN_PRIORITY = 1;      // 最低优先级
  14. var NORM_PRIORITY = 5;     // 正常优先级
  15. var MAX_PRIORITY = 10;     // 最高优先级

  16. // ==================== 日志悬浮窗初始化 ====================
  17. var logWindow = new floatUI();
  18. var logText = null;

  19. function initLogWindow() {
  20.     try {
  21.         logWindow.loadXML(
  22.             '<LinearLayout orientation="vertical" w="match_parent" h="match_parent" gravity="left">' +
  23.             '  <TextView id="logText" textColor="#00ff00" background="#000000" textSize="9" layout_width="wrap_content" layout_height="wrap_content" />' +
  24.             '</LinearLayout>'
  25.         );
  26.         
  27.         logText = logWindow.findViewById('logText');
  28.         
  29.         if (logText) {
  30.             setTimeout(function() {
  31.                 logWindow.setPosition(10, 100);
  32.             }, 100);
  33.             
  34.             logText.setText("AIWROK多线程综合示例\n");
  35.         } else {
  36.             print.log("日志视图未正确初始化");
  37.         }
  38.     } catch (e) {
  39.         print.log("创建日志悬浮窗失败: " + String(e));
  40.     }
  41. }

  42. // 更新日志显示
  43. function updateLog(content) {
  44.     print.log(content);
  45.    
  46.     try {
  47.         if (logText) {
  48.             var currentTime = new Date().toLocaleTimeString();
  49.             var logContent = logText.getText() || "";
  50.             var newLogContent = "[" + currentTime + "] " + content + "\n" + logContent;
  51.             // 限制日志行数,避免内容过多(增加到100行以填满全屏)
  52.             var logLines = newLogContent.split("\n");
  53.             if (logLines.length > 100) {
  54.                 newLogContent = logLines.slice(0, 100).join("\n");
  55.             }
  56.             logText.setText(newLogContent);
  57.         }
  58.     } catch (e) {
  59.         print.log("更新日志失败: " + String(e));
  60.     }
  61. }

  62. // ==================== 线程许可证工具 ====================
  63. function ThreadLicense() {
  64.     var _isLocked = false;
  65.     var _waitingQueue = [];
  66.    
  67.     this.acquire = function(callback) {
  68.         if (typeof callback !== 'function') {
  69.             return false;
  70.         }
  71.         
  72.         if (!_isLocked) {
  73.             _isLocked = true;
  74.             try {
  75.                 callback(this.release.bind(this));
  76.             } catch (e) {
  77.                 this.release();
  78.                 throw e;
  79.             }
  80.             return true;
  81.         } else {
  82.             _waitingQueue.push(callback);
  83.             return false;
  84.         }
  85.     };
  86.    
  87.     this.release = function() {
  88.         _isLocked = false;
  89.         if (_waitingQueue.length > 0) {
  90.             var nextCallback = _waitingQueue.shift();
  91.             this.acquire(nextCallback);
  92.         }
  93.     };
  94.    
  95.     this.isLocked = function() {
  96.         return _isLocked;
  97.     };
  98.    
  99.     this.getQueueLength = function() {
  100.         return _waitingQueue.length;
  101.     };
  102. }

  103. // 创建全局许可证用于数据库操作
  104. var dbLicense = new ThreadLicense();

  105. // ==================== 可暂停线程类 ====================
  106. function PausableThread(name, priority) {
  107.     var _thread = new thread();
  108.     var _isPaused = false;
  109.     var _isRunning = false;
  110.     var _name = name || "未命名线程";
  111.     var _priority = priority || NORM_PRIORITY;
  112.    
  113.     this.pause = function() {
  114.         _isPaused = true;
  115.         updateLog("[" + _name + "] 线程已暂停");
  116.     };
  117.    
  118.     this.resume = function() {
  119.         if (_isPaused) {
  120.             _isPaused = false;
  121.             updateLog("[" + _name + "] 线程已恢复");
  122.         }
  123.     };
  124.    
  125.     this.isPaused = function() {
  126.         return _isPaused;
  127.     };
  128.    
  129.     this.isAlive = function() {
  130.         return _thread.isAlive();
  131.     };
  132.    
  133.     this.stop = function() {
  134.         _isRunning = false;
  135.         _thread.stop();
  136.         updateLog("[" + _name + "] 线程已停止");
  137.     };
  138.    
  139.     var _checkPause = function() {
  140.         while (_isPaused && _isRunning) {
  141.             try {
  142.                 java.lang.Thread.sleep(100);
  143.             } catch (e) {
  144.                 updateLog("[" + _name + "] 暂停等待异常: " + String(e));
  145.             }
  146.         }
  147.     };
  148.    
  149.     this.start = function(taskFunction) {
  150.         _isRunning = true;
  151.         _isPaused = false;
  152.         
  153.         var threadName = _name;
  154.         var priority = _priority;
  155.         
  156.         _thread.runJsCode(function() {
  157.             try {
  158.                 // 设置线程优先级
  159.                 java.lang.Thread.currentThread().setPriority(priority);
  160.                
  161.                 var checkPause = _checkPause;
  162.                 var isRunning = function() { return _isRunning; };
  163.                
  164.                 taskFunction(checkPause, isRunning, threadName);
  165.             } catch (e) {
  166.                 updateLog("[" + threadName + "] 线程执行异常: " + String(e));
  167.             }
  168.         }, _name);
  169.         
  170.         updateLog("[" + _name + "] 线程已启动 (优先级:" + priority + ")");
  171.     };
  172.    
  173.     this.getName = function() {
  174.         return _name;
  175.     };
  176. }

  177. // ==================== 业务线程定义 ====================

  178. // 1. 主监控线程(高优先级)- 获取百度首页并监控状态
  179. function monitorTask(checkPause, isRunning, threadName) {
  180.     updateLog("[" + threadName + "] 开始获取百度首页");
  181.     var cycleCount = 0;
  182.    
  183.     while (isRunning() && cycleCount < 5) {
  184.         checkPause();
  185.         cycleCount++;
  186.         
  187.         try {
  188.             // 使用okHttp.get获取百度首页
  189.             var httpObj = new okHttp();
  190.             var response = httpObj.get("https://www.baidu.com");
  191.             if (response) {
  192.                 // 截取前100个字符显示
  193.                 var preview = String(response).substring(0, 100);
  194.                 updateLog("[" + threadName + "] 第" + cycleCount + "次请求: 成功");
  195.                 updateLog("[" + threadName + "] 响应预览: " + preview + "...");
  196.             } else {
  197.                 updateLog("[" + threadName + "] 第" + cycleCount + "次请求: 失败");
  198.             }
  199.         } catch (e) {
  200.             updateLog("[" + threadName + "] 第" + cycleCount + "次请求异常: " + String(e));
  201.         }
  202.         
  203.         try {
  204.             java.lang.Thread.sleep(1500);
  205.         } catch (e) {
  206.             updateLog("[" + threadName + "] 休眠异常: " + String(e));
  207.         }
  208.     }
  209.    
  210.     updateLog("[" + threadName + "] 监控任务完成");
  211. }

  212. // 2. 数据处理线程(中优先级)- 获取百度首页数据
  213. function dataProcessTask(checkPause, isRunning, threadName) {
  214.     updateLog("[" + threadName + "] 开始处理百度数据");
  215.     var processedCount = 0;
  216.    
  217.     while (isRunning() && processedCount < 5) {
  218.         checkPause();
  219.         processedCount++;
  220.         
  221.         // 使用许可证确保操作的原子性
  222.         dbLicense.acquire(function(release) {
  223.             updateLog("[" + threadName + "] 获取数据库许可证,处理第" + processedCount + "条数据");
  224.             
  225.             try {
  226.                 // 获取百度首页
  227.                 var httpObj = new okHttp();
  228.                 var response = httpObj.get("https://www.baidu.com");
  229.                 if (response) {
  230.                     // 截取前80个字符显示
  231.                     var preview = String(response).substring(0, 80);
  232.                     updateLog("[" + threadName + "] 第" + processedCount + "条数据获取成功");
  233.                     updateLog("[" + threadName + "] 响应: " + preview + "...");
  234.                 } else {
  235.                     updateLog("[" + threadName + "] 第" + processedCount + "条数据获取失败");
  236.                 }
  237.             } catch (e) {
  238.                 updateLog("[" + threadName + "] 第" + processedCount + "条数据异常: " + String(e));
  239.             }
  240.             
  241.             updateLog("[" + threadName + "] 第" + processedCount + "条数据处理完成");
  242.             release();
  243.         });
  244.         
  245.         try {
  246.             java.lang.Thread.sleep(1000);
  247.         } catch (e) {
  248.             updateLog("[" + threadName + "] 休眠异常: " + String(e));
  249.         }
  250.     }
  251.    
  252.     updateLog("[" + threadName + "] 数据处理完成,共处理" + processedCount + "条");
  253. }

  254. // 3. 日志记录线程(低优先级)- 获取百度首页并记录
  255. function logRecordTask(checkPause, isRunning, threadName) {
  256.     updateLog("[" + threadName + "] 开始记录百度访问日志");
  257.     var logCount = 0;
  258.    
  259.     while (isRunning() && logCount < 10) {
  260.         checkPause();
  261.         logCount++;
  262.         
  263.         try {
  264.             // 获取百度首页
  265.             var httpObj = new okHttp();
  266.             var response = httpObj.get("https://www.baidu.com");
  267.             if (response) {
  268.                 // 截取前60个字符显示
  269.                 var preview = String(response).substring(0, 60);
  270.                 updateLog("[" + threadName + "] 记录访问 #" + logCount + " - 成功");
  271.                 updateLog("[" + threadName + "] 响应: " + preview + "...");
  272.             } else {
  273.                 updateLog("[" + threadName + "] 记录访问 #" + logCount + " - 失败");
  274.             }
  275.         } catch (e) {
  276.             updateLog("[" + threadName + "] 记录访问 #" + logCount + " 异常");
  277.         }
  278.         
  279.         try {
  280.             java.lang.Thread.sleep(800);
  281.         } catch (e) {
  282.             updateLog("[" + threadName + "] 休眠异常: " + String(e));
  283.         }
  284.     }
  285.    
  286.     updateLog("[" + threadName + "] 日志记录完成,共记录" + logCount + "条");
  287. }

  288. // ==================== 线程管理 ====================
  289. // 创建三个不同优先级的线程
  290. var monitorThread = new PausableThread("监控线程", MAX_PRIORITY);
  291. var dataThread = new PausableThread("数据处理线程", NORM_PRIORITY);
  292. var logThread = new PausableThread("日志线程", MIN_PRIORITY);

  293. // 启动所有线程
  294. function startAllThreads() {
  295.     updateLog("========== 启动所有线程 ==========");
  296.    
  297.     monitorThread.start(monitorTask);
  298.     sleep.second(秒=0.2);
  299.    
  300.     dataThread.start(dataProcessTask);
  301.     sleep.second(秒=0.2);
  302.    
  303.     logThread.start(logRecordTask);
  304.    
  305.     updateLog("所有线程已启动");
  306. }

  307. // 暂停所有线程
  308. function pauseAllThreads() {
  309.     updateLog("========== 暂停所有线程 ==========");
  310.     monitorThread.pause();
  311.     dataThread.pause();
  312.     logThread.pause();
  313. }

  314. // 恢复所有线程
  315. function resumeAllThreads() {
  316.     updateLog("========== 恢复所有线程 ==========");
  317.     monitorThread.resume();
  318.     dataThread.resume();
  319.     logThread.resume();
  320. }

  321. // 停止所有线程
  322. function stopAllThreads() {
  323.     updateLog("========== 停止所有线程 ==========");
  324.     monitorThread.stop();
  325.     dataThread.stop();
  326.     logThread.stop();
  327. }

  328. // 监控线程状态
  329. function monitorThreadStatus() {
  330.     var isMonitoring = true;
  331.    
  332.     function checkStatus() {
  333.         if (!isMonitoring) return;
  334.         
  335.         try {
  336.             var mStatus = monitorThread.isAlive() ? "运行" : "停止";
  337.             var dStatus = dataThread.isAlive() ? "运行" : "停止";
  338.             var lStatus = logThread.isAlive() ? "运行" : "停止";
  339.             
  340.             var mPause = monitorThread.isPaused() ? "[暂停]" : "";
  341.             var dPause = dataThread.isPaused() ? "[暂停]" : "";
  342.             var lPause = logThread.isPaused() ? "[暂停]" : "";
  343.             
  344.             updateLog("状态 | 监控:" + mStatus + mPause + " | 数据:" + dStatus + dPause + " | 日志:" + lStatus + lPause);
  345.             
  346.             // 如果所有线程都已结束,停止监控
  347.             if (!monitorThread.isAlive() && !dataThread.isAlive() && !logThread.isAlive()) {
  348.                 isMonitoring = false;
  349.                 updateLog("所有线程已结束");
  350.                 return;
  351.             }
  352.             
  353.             // 继续下一次检查
  354.             setTimeout(checkStatus, 3000);
  355.         } catch (e) {
  356.             updateLog("监控线程状态出错: " + String(e));
  357.         }
  358.     }
  359.    
  360.     // 启动第一次检查
  361.     setTimeout(checkStatus, 3000);
  362. }

  363. // ==================== 自动演示流程 ====================
  364. function autoDemo() {
  365.     updateLog("========== 开始自动演示 ==========");
  366.     updateLog("演示场景: 多线程并发访问 https://www.baidu.com");
  367.     updateLog("线程1(高优先级): GET 百度首页 - 5次");
  368.     updateLog("线程2(中优先级): GET 百度首页 - 5次 (带许可证)");
  369.     updateLog("线程3(低优先级): GET 百度首页 - 10次");
  370.     updateLog("===============================");
  371.     updateLog("演示流程:");
  372.     updateLog("1. 启动3个不同优先级的线程");
  373.     updateLog("2. 3秒后暂停所有线程");
  374.     updateLog("3. 5秒后恢复所有线程");
  375.     updateLog("4. 8秒后再次暂停");
  376.     updateLog("5. 10秒后恢复");
  377.     updateLog("6. 15秒后停止所有线程");
  378.     updateLog("===============================");
  379.    
  380.     // 启动所有线程
  381.     startAllThreads();
  382.    
  383.     // 3秒后暂停
  384.     setTimeout(function() {
  385.         pauseAllThreads();
  386.     }, 3000);
  387.    
  388.     // 5秒后恢复
  389.     setTimeout(function() {
  390.         resumeAllThreads();
  391.     }, 5000);
  392.    
  393.     // 8秒后再次暂停
  394.     setTimeout(function() {
  395.         pauseAllThreads();
  396.     }, 8000);
  397.    
  398.     // 10秒后恢复
  399.     setTimeout(function() {
  400.         resumeAllThreads();
  401.     }, 10000);
  402.    
  403.     // 15秒后停止
  404.     setTimeout(function() {
  405.         stopAllThreads();
  406.     }, 15000);
  407. }

  408. // ==================== 程序入口 ====================
  409. initLogWindow();
  410. updateLog("AIWROK多线程综合操作示例");
  411. updateLog("版本: 1.0");
  412. updateLog("");

  413. // 延迟2秒后开始自动演示
  414. setTimeout(function() {
  415.     autoDemo();
  416.     monitorThreadStatus();
  417. }, 2000);
复制代码







欢迎光临 B2B网络软件 (http://bbs.niubt.cn/) Powered by Discuz! X3.2