服务器端js上的同步(node.js)

时间:2021-11-10 16:23:16

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

我愿意使用node.js实现一些服务器端代码。

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

node.js(js)是否内置了任何同步。就像我们一样

synchronized key word in java?

java中的同步关键字?

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

我可以使一些代码块同步吗?这样,只有线程才能执行它?

3 个解决方案

#1


11  

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.

在Node中,每个代码块都是同步的。 Node使用协作式多任务处理;唯一一段代码可以运行的时间是第一段代码返回的时间。

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.

这是它的事件驱动设计背后的推动力:你要求为你做一些缓慢的事情(例如从文件中读取),然后指定另一个在慢速操作完成时运行的函数。第一个函数返回,Node在等待I / O操作完成时可以运行其他函数。当I / O准备就绪,并且所有其他功能都已完成运行时,将调用您的继续。

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

当您完全控制代码何时产生时,不需要同步。实际上,每个功能都是同步的。

#2


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.

所以我认为你的问题有点过了..也许如果你提出一个问题,你试图解决这里的人可以指导你。

#3


1  

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

是的你可以使用光纤,更多细节http://alexeypetrushin.github.com/synchronize

#1


11  

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.

在Node中,每个代码块都是同步的。 Node使用协作式多任务处理;唯一一段代码可以运行的时间是第一段代码返回的时间。

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.

这是它的事件驱动设计背后的推动力:你要求为你做一些缓慢的事情(例如从文件中读取),然后指定另一个在慢速操作完成时运行的函数。第一个函数返回,Node在等待I / O操作完成时可以运行其他函数。当I / O准备就绪,并且所有其他功能都已完成运行时,将调用您的继续。

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

当您完全控制代码何时产生时,不需要同步。实际上,每个功能都是同步的。

#2


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.

所以我认为你的问题有点过了..也许如果你提出一个问题,你试图解决这里的人可以指导你。

#3


1  

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

是的你可以使用光纤,更多细节http://alexeypetrushin.github.com/synchronize