Does an asynchronous call always create / call a new thread?

Does an asynchronous call always create a new thread?

Example:

If JavaScript is single-threaded, how can it perform async postback? Does it really block until it receives a callback? If so, is this an asynchronous call?

+27
multithreading asynchronous
Feb 28 '09 at 18:29
source share
6 answers

This is an interesting question.

Asynchronous programming is a programming paradigm that is basically single-threaded, i.e. "after one thread of continuous execution."

You are referencing javascript, so let's discuss this language in a web browser environment. The web browser launches one javascript execution thread in each window, processes events (for example, onclick = "someFunction ()") and network connections (for example, xmlhttprequest calls).

<script> function performRequest() { xmlhttp.open("GET", "someurl", true); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { alert(xmlhttp.responseText); } } xmlhttp.send(sometext); } </script> <span onclick="performRequest()">perform request</span> 

(This is a non-working example, only to demonstrate concepts).

To do everything in asynchronous mode, the control thread has a so-called "main loop". The main loop looks something like this:

 while (true) { event = nextEvent(all_event_sources); handler = findEventHandler(event); handler(event); } 

It is important to note that this is not a β€œbusy cycle”. It is like a sleeping thread awaiting activity. The activity can be entered by the user (Mouse movement, button click, Enter), or it can be network activity (response from the server).

So in the example above

  • When the user clicks on the span, the ButtonClicked event will be generated, findEventHandler () will find the onclick event in the span tag, and then this handler will be called with the event.
  • When an xmlhttp request is created, it is added to the list of event sources all_event_sources.
  • After returning the function executeRequest (), mainloop waits for the next step nextEvent (), which is awaiting a response. At this point, nothing is blocked by further events.
  • Data is returned from a remote server, nextEvent () returns a network event, the event handler is the onreadystatechange () method, this method is called and the alert () dialog is called.

It is worth noting that alert () is a blocking dialog box. Until this dialog is completed, further events cannot be processed. This is the eccentricity of the javascript model of web pages, we have an easily accessible method that blocks further execution in the context of this page.

+50
Mar 05 '09 at 23:14
source share

The Javascript model is single-threaded. An asynchronous call is not a new thread, but rather interrupts an existing thread. It is similar to interrupts in the kernel.

Yes, it makes sense to have asynchronous calls with a single thread. Here's how to think about it: when you call a function within the same thread, the state for the current method is pushed onto the stack (i.e. local variables). The subroutine is called and finally returned, at which time the initial state is removed from the stack.

With an asynchronous callback, the same thing happens! The difference is that the subprogram is called by the system, not by the current code that calls the subprogram.

+15
Feb 28 '09 at 18:43
source share

A few notes about JavaScript in particular:

XMLHttpRequest is not blocked by default. The send() method returns immediately after the request has been relayed to the underlying network stack. The response from the server will schedule a call to your callback in the event loop, as discussed by other excellent answers.

This does not require a new thread. The main socket API can be selected, similar to java.nio.channels in Java.

You can build synchronous XMLHttpRequest objects by passing false as the third parameter to open() . This will cause the send() method to block until a response is received from the server, thus putting the event loop in dependence on network latency and a potentially freezing browser before the network timeout. This is Bad Thing β„’.

Firefox 3.5 introduces multi-threaded JavaScript with the honor of God with the Worker class. The background code runs in a completely separate environment and interacts with the browser window, scheduling callbacks in the event loop.

+5
Mar 07 '09 at 6:29
source share

In many GUI applications, an asynchronous call (such as Java invokeLater) simply adds a Runnable to the GUI thread queue queue. The GUI thread has already been created, and it does not create a new thread. But threads are not required even for an asynchronous system. Take, for example, libevent, which uses select / poll / kqueue, etc., to make non-blocking calls to sockets, which then calls callbacks for your code, completely without threads.

+4
Feb 28 '09 at 18:47
source share

No, but more than one thread will be involved.

An asynchronous call can start another thread to do the work, or send a message to the queue to another thread that is already running. The caller continues and the caller rings when he processes the message.

If you want to make a synchronous call in this context, you will need to send a message and actively wait until the callback occurs.

So, in short: more than one thread is involved, but this does not necessarily create a new thread.

+2
Mar 09 '09 at 16:03
source share

I do not know about javascript, but, for example, in the Windows Forms world, asynchronous calls can be made without multiple threads. This is due to how Windows Message Pump works. Basically, a Windows Forms application sets up a message queue through which Windows places event notification messages. For example, if you move the mouse, messages will be placed in this queue. A Windows Forms application will end up in an endless loop that will use all the messages that throw it away. According to what each message contains, it moves windows around, redraws them, or even calls custom methods, among other things. Method calls are defined by delegates. When the application finds the delegate instance in the queue, it happily calls the method passed by the delegate.

So, if you use a method that does something and want to create some asynchronous work without creating a new thread, all you have to do is put the delegate instance in the queue using the Control.BeginInvoke method. Now this is not multithreading, but if you queue very small pieces of work, it will look multithreaded. If, on the other hand, you give it a lot of time to execute, the application freezes until the method is executed, which will look like a jammed application, even if it does something.

+2
Mar 09 '09 at 18:10
source share



All Articles