|
|
AIWROKÈí¼þÑÝʾ¶àÏ̴߳´½¨ÔÝÍ£»Ö¸´
- /*
- 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);
¸´ÖÆ´úÂë
|
|