Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

multithreading - Can javascript run multiple functions at once?

Is it possible to call multiple functions at the same time?

E.g.

var executed = false;
// loop 1
func();
// loop 2
func();

function func(){
    if (executed) return;
    executed = true;
    alert(1);
}

Can func() be executed 2 times at once?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JavaScript has no native support for running multiple functions simultaneously. It has, historically, depended on time-consuming tasks (such as waiting for an HTTP request or doing something CPU intensive) being handled with native code that presents an API to JS (and that API accepting a callback or, more recently, returning a Promise).

That is starting to change.

Most web browsers support Web Workers and Node.js has introduced experimental support for Worker Threads.

These each allow JavaScript code to be farmed off to a separate process which runs independently of the main event loop and communicate with the main process using messages.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...