I noticed that there are submit methods for both Runnable task and Callable task. Why is there only invokeAll for Callable tasks, but no invokeAll for Runnable tasks. Thanks
Related
Suppose we have tow types of Task classes( First and Second )implementing Runnable. both of them have an access to a shared object (syncObj) which is supposed to be the Synchronization Object.
we want to use just one Thread pool with any number of thread in such a way that Task of the class First have higher priority to class Second in the Execution. using Countdown latch or any other object how could I implement this problem ?
I have read about Kotlin coroutines recently and now, I wonder what is the difference between asyncTask class and multi Thread programming and coroutines? in what situation I should use each one?
AsyncTask is abstract class and it must be subclassed. AsyncTask has 4 steps: onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
They are executed serially on single background thread.
If you want to fetch a URL or perform a heavyweight computation in Android, you have to use async programming.
they can be used when there is small task to communicate with main thread.
for tasks that use multiple instances in parallel.
Thread is a concurrent unit of execution. It has its own call stack.
With threads, the operating system switches running threads preemptively according to its scheduler.
they can be used for tasks running in parallel use Multiple threads.
for task where you want to control the CPU usage relative to the GUI thread.
Coroutines use to write asynchronous code that will look like normal sequential code.
they can provide a very high level of concurrency with very little overhead.
They are simple to read, unlike thread they are lightweight and unlike AsyncTask lot of them can run at same time.
AsyncTask was the first hand solution proposed by Google in Android SDK in order to process work in the background, while keeping the main thread free from many complex operations. In fact, AsyncTask let you to do complex processing in an asynchronous manner. Compared to classical Java Thread, the AsyncTask was somehow specialized, providing UI wrappers around threads in order to allow a more enjoyable experience as a developer, coding in an async way. The AsyncTask class was deprecated in the meantime and the recommended way to solve things is by using coroutines.
Coroutines are not a new concept introducer by Kotlin, in fact this concept exists in a lot of programming languages (Go has Goroutines and Java will provide something called Fibers). The main advantage of using coroutines is the simplicity of code, the only thing that differentiate a sync task/function in face of an async task/function is the usage of suspend keyword put in front of the function.
For example, the following function is executed in a synchronous way:
fun doSomething() = println("Print something")
while the following one is executed on a asynchronous way, due to the usage of suspend keyword:
suspend fun doSomething() = println("Print something")
When a suspend function is reached, the program will not block there, and will go further in running the rest of the code, but will receive a Continuation which will return the value computed by the suspended function when this one will be available.
I'm scheduling a long running task with Akka using the Runnable interface like this:
val myTask = system.scheduler.schedule(initialDelay, interval, new MyRunnable())
What exactly happens to the thread when Play is shutdown? I can see that the code in my runnable stops executing, but it's unclear to me at what point the thread is interrupted and how to hook into this event within my runnable. I have tried catching an InterruptedException (or any other) in my runnable but an exception is never thrown. What would be the best way to gracefully cancel the thread? I can call cancel on myTask but that just cancels the next scheduled execution. I thought about passing the lifecycle object to MyRunnable so that it could create a stop hook but I'm not sure this is the best approach.
I know the general theory, Thread, QRunnable and QThreadPool. How does it all fit in ? I mean when an instance of QRunnable is created, and assigned to the ThreadPool, what does it mean to start a thread ? Can multiple threads access the same QRunnable ? Does one QRunnable necessarily map one-to-one with one worker thread ?
QRunnable encapsulates a task that you want performed in a separate thread. If you need to know which thread is running that task or share it between threads, then you are probably doing something more complicated than what QThreadPool is designed to empower. In that case, you would create custom behavior using QThread directly. "Starting" a QRunnable with a QThreadPool is analogous to queueing that task for an available thread in the pool. Whereas, starting a QThread actually allocates a new OS thread and executes it.
The thread pool will manage a finite number of threads with a work queue of QRunnable instances. As a thread becomes available, it will be assigned a QRunnable to process. You don't need to explicitly create any QThread instances if you are using QThreadPool with QRunnable. Note that you still must ensure that shared resources are synchronized (e.g. with a QMutex, QMutexLocker, QReadWriteLock, QSemaphore, and/or QWaitCondition) when used in QRunnable instances.
I want my application to wait indefinitely until a task gets completed in another thread.
How do I perform this in Qt?
On windows, we use waitforsingletonobject, but is there any alternative to this?
Waiting for threads to finish certain tasks (thread synchronization) is the job of the QWaitCondition class.
Call wait on your QThread object.
Use QtConcurrent::run. See my answer to this question. Note that the QFutureWatcher API can work in blocking (the waitForFinished method) and non-blocking (the finished signal) modes.