I am trying to run 4 threads in parallel, but failing.
I have tried using synchronous wait()
and notify()
CyclicBarrier
ThreadPoolExecutor
CountDownLatch
AsyncTask
and many others, but didn't manage running the threads in parallel.
Can we run 4 threads in parallel (i.e. at same time in android)? How?
Note: I am working with Audio Streaming using AudioRecord
and AudioTrack
API's.
Working on multiple tasks at the same time is Multitasking. In the same way, multiple threads running at the same time in a machine is called Multi-Threading. Technically, a thread is a unit of a process. Multiple such threads combine to form a process.
With multithreading, while the computer system's processor executes one instruction at a time, different threads from multiple programs are executed so fast it appears the programs are executed simultaneously.
Android is a multiuser, multitasking system that can run multiple applications at the same time and let the user switch between applications without noticing a significant delay. The Linux kernel handles the multitasking, and application execution is based on Linux processes.
The answer is YES. Android is basically built upon Linux kernel which does utilize mulit-core. As far as single-threaded-application is concerned, remember that a thread can not be executed in-parts on different cores simultaneously.
Can we run 4 thread parallel i.e. at the same time in Android ??
Yes, you can run 4 thread in parallel. Adding more threads usually helps, but at some point, they cause some performance degradation. But 4 threads will not cause an issue.
Issues: I am not able to run thread parallel i.e. 1st Threads runs : 4 times, 2nd 3rd n 4th only 1 time. How to make it run (Tried using mentioned threads but unsuccess to run all thread parallel) parallel?
If your goal is simply to have all threads actively processing, I suggest looking at Java thread pools (and Executors more generally).
android support MULTI-THREADING? If yes how multi-threading works in android??
Yes, Android supports Multithreading. The most preferred way of implementing it(other than Thread Pool and Executor) is AsyncTask
AsyncTask allows you to implement doInBackground()
, where your thread can crank away at its task. This is similar to the functionality you'd get from Thread
.
The real magic happens when you override onPreExecute()
and onPostExecute()
, which are both executed on the UI thread. This should keep you from getting messages about your Activity not being attached.
this answer contains a small code example for AsyncTask
that could get you started.
Also, have a look at Handlers in Android
Edit- What I found during my research is that recording, sending and receiving audio all are quite interactive processes and they require sync and IO resources. The best possible solution for it could be, you can create separate AsyncTask for each of the operations so that each operation is performed in its separate AsyncTask.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With