Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does multi-threading work in Android?

I am trying to run 4 threads in parallel, but failing.

I have tried using synchronous wait() and notify() CyclicBarrier ThreadPoolExecutor CountDownLatch AsyncTaskand 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.

like image 802
Rahul Baradia Avatar asked Dec 27 '12 11:12

Rahul Baradia


People also ask

How does multithreading work in Android?

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.

How does multi threading works?

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.

Does multitasking multi threading support Android?

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.

Do phones use multithreading?

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.


2 Answers

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.

like image 66
Sahil Mahajan Mj Avatar answered Sep 28 '22 00:09

Sahil Mahajan Mj


  1. You can run 4 different Threads, but in order get executed in parallel (simultaneously) you arch have to support it. If you have a single core cpu, the order which threads are excuted is up to the scheduler and only one thread at time will be executed
  2. The order and the timing of the execution are up to the scheduler.
  3. yes it does. You can rely both on AsyncTask or the java features (Executors eg)
like image 35
Blackbelt Avatar answered Sep 28 '22 00:09

Blackbelt