Method to get the running thread id in javascript/jquery
Asked Answered
H

4

10

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

Hesse answered 2/9, 2010 at 21:39 Comment(0)
C
5

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?

Cyb answered 2/9, 2010 at 21:42 Comment(4)
Actually what i want is.. In my java script 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 processed the data.So here i am trying to see how it works in way that.. lets say there are 10 msgs in the channel so for each message the call back is called .Hesse
Lets say the callback method is running for a message "a" and while processing the data for the message "a" it got another message "b" .Is that the call back method is called for the message "b" once it is done with processing for message "a" ...?I wanted to check this by printing the thread number in the call back function which tells it is running sequentially (one thread) or multiple threads.. That is why i was trying to print the thread id.. Thanks for your responses..Hesse
@user436175 - You should update your question with the comments you have made here. That is a very interesting point, and surprisingly it is possible to have "b"'s callback execute while your "a" is still running in rare cases. There was a recent question about this with a detailed explanation which I will link to if I can find it. Also, when you say you are listening to a channel, are you referring to WebSockets by any chance?Cyb
No it is not a websocket.It is the cometd channel. Cometd is a new technology in the market which is asynchronous to client.It is reverse ajax.Browser client subscribes to the channel and the server pushes the data to the channel and who ever subscribed they get this message just like pub/sub in JMSHesse
K
1

For most things in JavaScript there's one thread, so there's no method for this, since it'd invariable by "1" where you could access such information. There are more threads in the background for events and queuing (handled by the browser), but as far as your code's concerned, there's a main thread.

Java != JavaScript, they only share 4 letters :)

Knossos answered 2/9, 2010 at 21:41 Comment(2)
Also see What's the difference between JavaScript and Java?Cordeiro
Of course they are not != but since Java letters are completely in JavaScript. :)Bullish
H
1

Aside from the name, Javascript is totally unrelated to Java. Javascript does not have threads that you can access.

Hybrid answered 2/9, 2010 at 21:42 Comment(0)
A
0

In javascript the scripts run in a browser thread, and your code have no access to that info, actually your code have no idea whatsoever how it's being run. So NO! there's no such thing in javascript.

Andraandrade answered 2/9, 2010 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.