Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to get the running thread id in javascript/jquery

I am new to javascript/jquery. I have a simple question one of java script function is running and wanted to see the thread id for that thread. In java we do like

Thread.getID();//in java

which will print the thread id of running thread. In the similar way what is the function we use to get the running thread id in javscript.

Actually what i want is..

In my JavaScript, I have a listener which is listening to the channel. When ever there is a message in the channel, the callback method is called and processes the data. So here I am trying to see how it works in that way.. Let's say there are 10 messages in the channel and for each message the callback is called.

Let's say the callback method is running for a message "a" and while processing the data for the message "a", it got another message "b". Will the callback method for "b" be called once the processing for message "a" is complete?

I wanted to check this by printing the thread number in the callback function which tells whether it is running sequentially (one thread) or multiple threads. That is why I was trying to print the thread id. Thanks for your responses.

Thanks, Swati

like image 716
swati Avatar asked Sep 02 '10 21:09

swati


People also ask

How do I find my current thread ID?

In the run() method, we use the currentThread(). getName() method to get the name of the current thread that has invoked the run() method. We use the currentThread(). getId() method to get the id of the current thread that has invoked the run() method.

Does a thread have an ID?

In some threads implementations, the thread ID is a 4-byte integer that starts at 1 and increases by 1 every time a thread is created. This integer can be used in a non-portable fashion by an application.


1 Answers

JavaScript is single threaded. So this wouldn't apply to JavaScript.

However, it is possible to spawn multiple threads through a very limited Worker interface introduced in HTML5 and is already available on some browsers. From an MDC article,

The Worker interface spawns real OS-level threads, and concurrency can cause interesting effects in your code if you aren't careful. However, in the case of web workers, the carefully controlled communication points with other threads means that it's actually very hard to cause concurrency problems. There's no access to non-thread safe components or the DOM and you have to pass specific data in and out of a thread through serialized objects. So you have to work really hard to cause problems in your code.

What do you need this for?

like image 62
Anurag Avatar answered Sep 19 '22 14:09

Anurag