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

Is javascript single-threaded? - Stack Overflow

programmeradmin0浏览0评论

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?

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 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?

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?

Share Improve this question edited May 23, 2017 at 11:53 CommunityBot 11 silver badge asked Mar 15, 2012 at 18:42 PawelRomanPawelRoman 6,2925 gold badges32 silver badges43 bronze badges 2
  • 2 There may be race conditions due to network latency if you're working with ajax, but not because of JavaScript itself because it's single-threaded. – pimvdb Commented Mar 15, 2012 at 18:45
  • Not quite always: stackoverflow./questions/2734025/… – phazei Commented Mar 28, 2013 at 18:42
Add a ment  | 

2 Answers 2

Reset to default 4

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

Yes.

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 is single threaded.

The referenced post discusses differences of setTimeout on different machines...


HTML5 introduces the concept of WebWorkers which executes JavaScript on multiple background threads. Though it is not supported on all browsers...

https://developer.mozilla/En/Using_web_workers

http://dev.w3/html5/workers/

发布评论

评论列表(0)

  1. 暂无评论