11

I am willing to implement some server side code using node.js.

Does node.js (js) have any synchronization inbuilt.Like we have

synchronized key word in java?

Can i make some code block synchornized?so that at one time only on thread can execute it?

2
  • 2
    JavaScript is generally not multi-threaded. Commented Feb 3, 2012 at 12:27
  • Can you provide an example of problematic code you're trying to fix? Commented Nov 23, 2019 at 13:03

3 Answers 3

14

In Node, every code block is synchronized. Node uses cooperative multitasking; the only time another piece of code can run is when the first piece of code returns.

That's the driving force behind its event-driven design: you ask for something slow to be done for you (e.g. reading from a file), and then you specify another function to be run when that slow operation is done. The first function returns, and Node can run other functions while it's waiting for the I/O operation to be done. When the I/O is ready, and all other functions are done running, then your continuation will be called.

Synchronization isn't needed when you're in full control of when your code will yield. In effect, every function is synchronized.

Sign up to request clarification or add additional context in comments.

3 Comments

@Mir-Ismaili How so? If you're not doing much parallel processing, then you're probably not looking at much more bookkeeping than you'd already have for using JavaScript in the first place. But if you're, say, writing a web server, then event-driven scales way better than the "one thread per request" model that predated it, because it has so much less overhead - fewer thread switches, less time in kernel mode, not to mention way less memory allocated for thread stacks.
Thanks. Yes, when we comparing with "one thread per request". But "one thread per request" is not a good criterion, because of it's poor design. I came from Java and I especially like to compare with thread-pool methods. For clarity, doesn't this feature of Javascript causes it to run all things on a single thread?
@Mir-Ismaili Sure, you could think of it like a thread pool with one thread. But every time you do something that could block, like file or network I/O, you yield that thread to the next task; so that thread only spends time running your code, never waiting on the OS. So if your code does much file or network I/O, I'd guess Node would scale far better than a Java thread-pool thread. Of course you could still reach a limit where that thread is 100% utilized running your code; in that case, I think the usual thing is to spin up multiple processes or multiple containers.
1

Node does not use threads. It is based on an event machine...

So I think your question is a little off.. Maybe if you give a problem that you are trying to solve people here can guide you.

Comments

0

Yes You can do it with fibers, more details here http://alexeypetrushin.github.com/synchronize

1 Comment

This is a different thing. The question is about producing code (to be thread-safe when the consumer uses it), not about consuming other codes (that may be run on another thread).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.