0

It seems logical an asynchronous action must be executed in a different thread. That is to say, no asynchronous action can't be created without involving multi-threading. After all, if it's not executing in a different thread, it would block the main thread and therefore it wouldn't be asynchronous anymore.

However, they told me that asynchronous doesn't necessarily mean involving additional threads. If I recall correctly, they told it was true at least for javascript.

So my question is: does asynchronous operation have to execute in a different (not main) thread always and can asynchronous computation exist without multi-threading (a separate thread for it)?

1
  • Threads allow asynchronous computation because the OS schedules them, but other scheduling mechanisms are possible. Go, for instance, uses goroutines, which are lightweight "threads" (though not in the OS-sense) that the go runtime schedules concurrently. Commented Nov 23, 2013 at 14:22

4 Answers 4

3

You haven't really specified a language/technology here.

asynchronous traditionally means it runs independent of a "clock" synonyms are "event based" - asynchronous just means something is triggered from an event. Synchronous is regular code flow like reading one line after the other sequentially.

An async function can happen in any thread, you are comparing apples and oranges it seems.

Think of a thread forked to monitor interrupts, the monitoring thread would be used asynchronously if an interrupt occurred. It is a strange question but the answer to it is "no, asynchronous activities can happen in any thread"

And yes, it can exist without multithreading.

Sign up to request clarification or add additional context in comments.

2 Comments

how can it exist without multithreading if code executes line by line?
Check out event loops - basically your code can idle in a "listening" state and when an event/interrupt occurs it can execute code. All single threaded.
1

You can have one OS thread but do asynchronous actions using multiple green threads (threads that are managed in userspace). One context where this is relevant is IO bound applications, such as a web application hitting a database. In general this is IO bound by the network that connects to the database.

Suppose we only have one thread of control. If requests are made synchronously, then only one request happens at a time because we wait for the socket to return a packet to us before moving to the next request. Now you can realize that most time is spent sitting for the response, so put this worker in a "wait queue" (using select or epoll or what have you), and let someone else get processed and wait.

The person responsible for doing this is the scheduler or event loop (see green threads as well which are light weight but managed in user space and not by the kernel). So the idea is to write a scheduler that divys up the time on that one thread itself.

Maybe you are familiar with python or node js? Some web frameworks like tornado and twisted take advantage of a single thread which gets switched by an "event loop": see here: http://krondo.com/?p=1209.

Comments

1

Asynchronicity and multithreading are different concepts, though often confused. To support asynchronicity, one thread is enough, and it can be the main thread, but switched to asynchronous mode. Asynchronicity on multiple threads is possible but is more complex and error-prone. So typical decision is to dedicate a separate thread for asynchronicity, like it is done in all GUI frameworks, including swing/awt.

Comments

0

It depends on requirement but it will be better to separate thread for Asynchronous computation. E.g In http application, if main thread is waiting for response from some other server and will not do anything great after receiving it, it will be better to start a new thread for this, let the main thread finish it's job and release all resources acquired by it. It will improve performance and reduce heap usage also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.