0

I am currently working on an android project written in java. I have a dashboard that queries data from a cloudant database and renders the data on graphs. the data however has to be processed when received.
I have 4 AsyncTasks that process the received data in the doInBackground Override method simultaneously (or is supposed to). The process being very slow, I tried the line

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

in each AsyncTask.
However, the 4 AsynkTasks seem to all happen one after the other, is that due to changing the priority of the threads to max? When a thread priority is changed, does it stop all other threads and continue with just the one until it finishes?

Michael Vaquier
  • 1,621
  • 2
  • 10
  • 19
  • From the JavaDoc on `Thread`: "Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. " – Thomas Jul 25 '17 at 14:22
  • Even if you change the priority, unless they are really small task they should still show some degree of parallelism. Post more code to see if there is some error somewhere else. – bracco23 Jul 25 '17 at 14:24

2 Answers2

3

From: https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

(as a note: this information was found by searching "java Thread.MAX_PRIORITY" in google and looking at the top result)

Marshall Tigerus
  • 3,675
  • 10
  • 37
  • 67
2

From the documentation:

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. ... When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread.

It's literally just a hint to tell the scheduler roughly what order to execute the threads. It can, and likely will, ignore you.

If you've set the priority of all threads to the maximum, that's effectively the same as leaving them at the default. As you may have to tell your boss, if everything is top priority then nothing is top priority :)

Michael
  • 41,989
  • 11
  • 82
  • 128