How to release frozen/locked thread?

Asked

Viewed 356 times

14

1 answer

5


There’s no such thing as unlock, your script is an infinite loop case and can not stop without finishing the whole process, because javascript runs in a single event and what probably froze was the process and not only Thread, in some questions I commented on something similar:

The technique cited with setTimeout for example they only help to minimize the problem (in the case of your script is more a mistake than a problem :) ), but in these two responses I also quoted:

That solutions are possible for what you want to achieve. jQuery for example uses AMD, see this is a part of jquery:

if ( typeof define === "function" && define.amd ) {
    define( "jquery", [], function() {
        return jQuery;
    });
}

However what will most likely help you is the web Workers that as the documentation actually runs on a separate Thread from the browser:

Web Workers provide a simple Means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface

Translating: Web Workers provide a simple means for web content to run scripts to run Thread scripts in the background. The worker thread can run tasks without interfering with the user interface.

Example with web Workers

To create a process with web Works it is necessary to create a file . js (let’s call it meujavascript.js) separate and call it so:

var meuWorker = new Worker("meujavascript.js");

To capture script responses meujavascript.js, you will have to use onmessage:

meuWorker.onmessage = function(e) {
   console.log('Mensagem recebida do Worker', e.data);
};

To send data to meuWorker.onmessage in the content of meujavascript.js you must use:

postMessage("mensagem");

You can also send data to meujavascript.js thus:

var worker = new Worker("meujavascript.js");
worker.postMessage('Olá mundo!');

And in the content of meujavascript.js you should add something like:

onmessage = function(e) {
    console.log('Mensagem recebida do MAIN', e.data);
};
postMessage("mensagem");

Now comes the answer, you can "finish" (kill, Kill, etc) a webworker, using terminate(), in this way:

var worker = new Worker("meujavascript.js");
worker.postMessage('Olá mundo!');
worker.terminate();

So you can work with a setTimeout in the main (outside the worker) and check whether it has been a long time since the webworker sent a message, there has probably been a freeze, so if the time of the setTimeout expire you execute the terminate.

  • Pass a task to the web worker can improve performance or will only be dividing the processing capacity, can decrease performance?

  • @Guilhermecostamilam pass the task to the worker if necessary, independent of performance, which by the way will be relative and will vary a lot of how your specific code works and not the Worker or the main process.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.