12

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.

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121

2 Answers2

17

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.

exploitr
  • 843
  • 1
  • 14
  • 27
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • Thanks.. But when I am trying AsyncTask, I am loosing many audio data. i.e. I am sending audio voice data to server using AsyncTask,its taking its own time to send it. By that time I ll loose voice data on app side. – Rahul Baradia Dec 27 '12 at 12:02
  • I haven't faced any problem with Async task though, still if you want to use concurrent threads, you could have a look at thread pool and executors. – Sahil Mahajan Mj Dec 27 '12 at 12:07
  • I tried that too.. I am recording audio data 16Kb's per 40ms. Its taking time to run another thread parallel. – Rahul Baradia Dec 27 '12 at 12:10
  • what else you are running parallely along with recording audio. there might be some synchronization issues. – Sahil Mahajan Mj Dec 27 '12 at 12:13
  • sending thread and receiving thread audio voice to-from server using HTTP-CLIENT – Rahul Baradia Dec 27 '12 at 12:16
  • Not sure, but there might be some sync issues, the thread might have occupied resources that other threads need. – Sahil Mahajan Mj Dec 27 '12 at 12:18
  • that is happening while recording.. It holds the thread till records the voice of 16kb and prblm with HttpClient too. Takes around 250ms to run. – Rahul Baradia Dec 27 '12 at 12:19
  • Is this some kind of audio chat app. so, you are saying first it records all the audio and then it receives the audio voice from the server. and what's the problem with httpclient. – Sahil Mahajan Mj Dec 27 '12 at 12:24
  • its Streaming audio. It records,sends,receive and plays.All are in different Threads. Want to run parallel or concurrently. How should I achieve this. ?? – Rahul Baradia Dec 27 '12 at 12:34
  • 1
    there might be a deadlock situtation because all of the operations you mentioned are so interactive and they all require the use of system resources. I will have a look at how can we solve this and i'll get back to you. but it is really a complex task to do. – Sahil Mahajan Mj Dec 27 '12 at 12:39
  • yeah exactly, its very complicated task & not able to do it. Please let me know,if you get any Ideas. – Rahul Baradia Dec 27 '12 at 12:41
  • have you got it, any idea or solutions ?? – Rahul Baradia Dec 28 '12 at 12:38
  • I have tried this today. AsyncTask is taking to much time to perform it. Thanks – Rahul Baradia Dec 28 '12 at 13:10
2
  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)
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • According to first point does that mean only one thread can run on single core? what about concurrent running of thread's? – Anmol Dec 04 '19 at 15:29