0

I'm have a handler thread with thread priority set to background,

myHandlerThread = new HandlerThread(HANDLER_THREAD_NAME, Process.THREAD_PRIORITY_BACKGROUND);

On this myHandlerThread I'm doing some operation to alter system property by invoking

SystemProperties.set(property, propertyValue);

I'm facing this exception frequently,

[Events]
java.lang.RuntimeException: failed to set system property
at android.os.SystemProperties.native_set(Native Method)
at android.os.SystemProperties.set(SystemProperties.java:130)

There can be multiple reasons which can cause this

1) The native code might have timed out when executing this code.

2) Since the thread is given with priority BACKGROUND it might not have time to execute when the CPU is busy and might have timed out at the end.

Will increasing the thread priority to NORMAL would help? Or how the thread will get timed out? Or what are the other reasons which could cause this issue?

gooogle
  • 375
  • 4
  • 16

1 Answers1

2
    myHandlerThread = new HandlerThread(HANDLER_THREAD_NAME,
Process.THREAD_PRIORITY_BACKGROUND);

Value of Process.THREAD_PRIORITY_BACKGROUND = 10

As you already know that in Java each thread has its own priority based on the thread which has created it. And we can change these priorities also like your above code.

By default, java provides priority scale between 0-10. But we can increase the scale by using Process.setThreadPriority() which provides scaling between (+/- 20).

Runnable r = ...
Thread thread = new Thread( new Runnable() {
   public void run() {
     android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE);
     r.run();
   }
});

So currently your above code is equivalent to Thread.setPriority() which only sets the handler thread priority to 10 (least in scale(0-10)).

I think the first one could be the reason which you had mentioned above:

1) The native code might have timed out when executing this code.

  • So, can there be reason like the thread didn't get turn in CPU to execute it's instructions? Since the next idle thread to be picked up is based on priority. What is the timeout value that's associated with the thread? Or when the thread will be killed if it waited for sooo long time and didn't get chance to execute (starvation)? – gooogle Sep 11 '19 at 19:06
  • That you can check by printing your logs in the thread whether it is executing or not. But I don't thing that it is happing according to your above system logs. – Deependra Kumar Sep 11 '19 at 19:12