最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - synchronization on server side js (node.js) - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Feb 3, 2012 at 12:25 user93796user93796 18.4k31 gold badges98 silver badges153 bronze badges 2
  • 2 JavaScript is generally not multi-threaded. – Pointy Commented Feb 3, 2012 at 12:27
  • Can you provide an example of problematic code you're trying to fix? – Ivan Rubinson Commented Nov 23, 2019 at 13:03
Add a comment  | 

3 Answers 3

Reset to default 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.

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.

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

发布评论

评论列表(0)

  1. 暂无评论