0

I have several threads in my Android application that should be executed concurrently. I am not running them on several cores. REAL parallel execution at exactly the same time is not necessary. If Android switches between the different tasks or temporarily pauses certain threads, it's Ok.

The problem: Some threads are highly time consuming and computationally expensive (complex algorithms), but are not real time critical. Other threads and the Android UI thread are real time critical and should not be blocked or heavily delayed by the time consuming processes. Nevertheless, the time consuming processes should also be executed if there are no other more important tasks to perform. Ideally, the highly important threads should safely pause the less important threads. In Java people used to implement suspend() commands, but they are now depricated.

What is the recommended way to solve this problem in Android?? In Java I would have used Sleep commands or wait and notify methods. What is recommended way in Android?

Thanks for the anwer, guys!

Edit: I was thinking about threads. But I wrote "processes" instead of "threads", just in case regular threads are not the best way to solve the problem in Android. I am open to anything. I have only ONE App. Sorry if terms get mixed up a little. But I wanted to keep the options and ideas open.

Thanks for the comments and answers so far. But I am more interested in a GENERAL, recommended, established strategy and good practice to solve this kind of problem in Android. I could also do some hacks, but I was really hoping for a clean and established solution. I am more looking for an answer like: "The common, established approach is to use strategy A + strategy B..."

Nir Duan
  • 6,164
  • 4
  • 24
  • 38
Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

0

In my opinion you should use a different component (Service) and set it to run on a different process - here you can see how
By doing so you can put all the computing in a different process that not related to your man UI component lifecycle.

If the dependency of algorithms' logics and the main UI is loose I wouldn't try to implement them via Threads concurrency.

Community
  • 1
  • 1
Nir Duan
  • 6,164
  • 4
  • 24
  • 38
  • if i called it from inside the Asynctask, would it mean that the thread of the asynctask is set to Thread.MAX_PRIORITY? – Amrmsmb Jul 19 '16 at 10:42
  • It won't work, Async task are thread pool inside your process, check this answer: http://stackoverflow.com/questions/12039596/asynctask-on-executor-and-priorityblockingqueue – Nir Duan Jul 19 '16 at 11:15
  • I have edited my original question. I am mostly interested in good practice and general strategy to solve this kind of problem, not necessarily a specific command. – Amrmsmb Jul 21 '16 at 08:31
  • @user2121 I edit my answer to better fit the question – Nir Duan Jul 21 '16 at 10:27