javascript 如何挂起Web工作进程

plupiseo  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(117)

我试着浏览了一下网络工作者的文档,虽然我没有找到我想要的东西,但文档也不是很好,所以我想我还是问问吧。
我正在开发一个使用Cordova的移动的应用程序,它可以与外部设备通信。我需要每秒向外部设备发送一些命令。为此,我设置了Web Workers,它基本上相当于JavaScript中的线程。但是,有一个命令必须绕过外部设备才能运行。也就是说,如果设备在处理命令时收到其他命令,命令将失败。2为此,我只想挂起我的线程。
在Java中这很容易。有一个公共的void线程。(),然后,稍后,线程.resume().我的自然倾向是WebWorkers将此作为默认函数。Worker.suspend(),工人.简历(),但我找不到任何文档支持Workers具有这种功能的想法,尽管这是多线程的一个常见期望。是否存在这样一个固有功能?如果没有,是否有实施类似职能的最佳做法?

3okqufwl

3okqufwl1#

此代码段显示如何
1.创建三个Web工作线程
1.从Web工作线程获取计数器值
1.暂停Web工作线程
1.检测所有三个Web工作线程何时停止
1.重新启动Web工作进程
由于StackOverflow代码段的限制,Web辅助程序代码是内联的。

function getInlineJS() {
    var js = $('[type="javascript/worker"]').text();
    var blob = new Blob([js], {"type": "text/plain"});
    return URL.createObjectURL(blob);
}

var worker_list = [];
var worker_num = 3;
var pause_count = null;

function create_workers() {
    var index;
    for (index = 0; index < worker_num; index++) {
        var web_worker = new Worker(getInlineJS());
        var counter_selector = '#counter' + (index + 1);
        (function(selector) {
            web_worker.onmessage = function(event) {
                var paused = event.data['paused'];
                if (paused) {
                    pause_count++;
                }
                if (pause_count == worker_num) {
                    // At this point we know the workers have all stopped
                    $('#pause_count').text(pause_count);
                }
                var counter = event.data['counter'];
                $(selector).text(counter);
            }
        })(counter_selector);
        worker_list.push(web_worker);
    }
}

/**
 * Send a bogus message to get the workers to send their counters
 */
function update_counters() {
    var index;
    for (index = 0; index < worker_num; index++) {
        var message = {'index': index};
        worker_list[index].postMessage(message);
    }
}

/**
 * Send the pause flag to the workers
 * @param pause_state
 */
function pause_counters(pause_state) {
    var index;
    for (index = 0; index < worker_num; index++) {
        var message = {'pause_flag': pause_state};
        worker_list[index].postMessage(message);
    }
}

var pause_state = false;
$(document).ready(function() {
    create_workers();
    setInterval(update_counters, 1000);
    $('#pause').on('click', function() {
        pause_state = !pause_state;
        if (pause_state) {
            pause_count = 0;
        }
        else {
            $('#pause_count').text('');
        }
        pause_counters(pause_state);
        $('#pause').text(pause_state ? 'Start' : 'Stop');
    });
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Web Worker</title>
</head>
<body>
    <div class="counter">
        Counter 1: <span id="counter1"></span>
    </div>
    <div class="counter">
        Counter 2: <span id="counter2"></span>
    </div>
    <div class="counter">
        Counter 3: <span id="counter3"></span>
    </div>
    <div class="button">
        <button id="pause" type="button">Stop</button>
    </div>
    <div>
        Pause Count: <span id="pause_count"></span>
    </div>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.js"
        integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
        crossorigin="anonymous"></script>
<script src="sync.js"></script>
<script type="javascript/worker">
var pause_flag = false;
var counter = 0;

function run_command() {
    if (!pause_flag) {
        counter++;
    }
}

var interval = setInterval(run_command, 1000);

onmessage = function(event) {
    if ('pause_flag' in event.data) {
        pause_flag = event.data['pause_flag'];
    }
    var message = {
        "paused": pause_flag,
        "counter": counter
    };
    postMessage(message);
};
</script>
</html>

相关问题