是javascript单线程吗? [英] Is javascript single-threaded?

查看:143
本文介绍了是javascript单线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript是否在单线程上运行?如果我声明一个全局数组,并开始在AJAX回调上更改它(例如删除元素),同时开始在另一个函数中更改相同的数组(使用SetTimeOut调用) - 是否存在竞争条件的风险?

Is javascript running on single thread? If I declare a global array, and start changing it (eg removing elements) on AJAX callback and at the same time start changing the very same array in another function (called with SetTimeOut) - is there a risk of a race condition?

我找到了这个帖子: javascript竞争条件,有人说javascript中的竞争条件永远不会发生,因为它总是在单线程和单个callstack中运行。这取决于浏览器如何实现它,还是保证在所有浏览器上始终是单线程的?

I found this thread: javascript race condition, someone said race conditions never happen in javascript because it always runs in single thread and single callstack. Does this depend on how browser implements it or is it guaranteed to be always single-threaded on all browsers?

推荐答案


是否保证在所有浏览器上始终是单线程的?

is it guaranteed to be always single-threaded on all browsers?

是。

当然,HTTP请求之类的东西可能在幕后的不同线程中有效,但是当你的Javascript代码被执行时,它一次只能从一个线程发生。

Of course, things like HTTP requests might work in different threads behind the scenes, but when your Javascript code is executed it can only happen from one thread at a time.

这篇关于是javascript单线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆