是否异步编程方法 - 多线程? [英] Does Async programming means - multi Threading?

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

问题描述

让我们谈谈JavaScript的code具有的setInterval 方法,每个 2 秒。

lets talk about JavaScript code which has setInterval methods every 2 sec.

(写一些文字...请不要介意...)。

( writing some text ...nevermind...).

我也有一个的onblur动画事件有所控制。

I also have a onBlur animation event for some control.

在一个情况下的onblur occures(=动画) - 我可能会得到的setInterval 功能

in a case where onblur occures (= animation) - I might get the setInterval function.

所以我的问题是:

是否异步编程装置 - 多线程? (在某种程度上)?

Does Async programming means - multi Threading ? ( in some way ) ?

我知道的js是不是多线程的语言。

I know that Js is not a multiThreading language.

所以..?

推荐答案

没有。它字面上的意思是什么是指:异步的。了解异步编程和基于线程的编程的区别就是你的成功作为一个程序员是至关重要的。

No. It means literally what it means-- asynchronous. Understanding the difference between asynchronous programming and thread-based programming is critical to your success as a programmer.

在一个传统的,非线程环境中,当一个函数必须等待外部事件(如一个网络事件,键盘或鼠标事件,甚至时钟事件),程序必须的直到事件发生。

In a traditional, non-threaded environment, when a function must wait on an external event (such as a network event, a keyboard or mouse event, or even a clock event), the program must wait until that event happens.

在多线程环境中,编程的许多个体线程同时运行。 (取决于CPU的数量和操作系统的支持下,这可能是完全正确的,或者它可以是由复杂的调度算法创建的错觉)。由于这个原因,多线程环境是困难的,涉及螺纹锁紧彼此的存储器到$ P $的问题从超速彼此pvent它们。

In a multi-threaded environment, many individual threads of programming are running at the same time. (Depending upon the number of CPUs and the support of the operating system, this may be literally true, or it may be an illusion created by sophisticated scheduling algorithms). For this reason, multi-threaded environments are difficult and involve issues of threads locking each other's memory to prevent them from overrunning one another.

在一个台异步环境下,一个单一的过程线程运行所有的时间,但它可以是,事件驱动的原因(这是关键)从一个功能到另一个交换机。当一个事件发生时,的当当前运行的进程打在这一点,它必须等待另一个事件的,javascript的核心将扫描活动的名单,并提供下一个,在(正式)不确定的(但可能确定​​的)秩序,事件管理器。

In an asychronous environment, a single process thread runs all the time, but it may, for event-driven reasons (and that is the key), switch from one function to another. When an event happens, and when the currently running process hits a point at which it must wait for another event, the javascript core then scans its list of events and delivers the next one, in a (formally) indeterminate (but probably deterministic) order, to the event manager.

由于这个原因,事件驱动,异步编程避免了许多传统的,多线程编程的缺陷,如内存争问题。有可能仍然是竞争条件,在这事件的处理顺序并不取决于你,但他们是罕见的,更易于管理。而另一方面,因为事件处理程序不传递事件,直到当前正在运行的功能击中空闲点,有些功能饿死节目的其余部分。这发生在Node.js的,例如,当人们愚蠢地做很多繁重的数学在server--是最好的推到一个小服务器节点,然后等待提供了答案。 Node.js的是一个伟大的小交换机的事件,但任何时间超过100毫秒应该在客户端/服务器的方式来处理。

For this reason, event-driven, asynchronous programming avoids many of the pitfalls of traditional, multi-threaded programming, such as memory contention issues. There may still be race conditions, as the order in which events are handled is not up to you, but they're rare and easier to manage. On the other hand, because the event handler does not deliver events until the currently running function hits an idle spot, some functions can starve the rest of the programming. This happens in Node.js, for example, when people foolishly do lots of heavy math in the server-- that's best shoved into a little server that node then "waits" to deliver the answer. Node.js is a great little switchboard for events, but anything that takes longer than 100 milliseconds should be handled in a client/server way.

在浏览器环境中,DOM事件被视为自动事件点(他们必须,修改DOM提供了大量的事件),但即使有不好写JavaScript可以饿死的核心,这就是为什么Firefox和Chrome浏览器有这些此脚本已停止响应中断处理程序。

In the browser environment, DOM events are treated as automatic event points (they have to be, modifying the DOM delivers a lot of events), but even there badly-written Javascript can starve the core, which is why both Firefox and Chrome have these "This script is has stopped responding" interrupt handlers.

这篇关于是否异步编程方法 - 多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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