Threading does not act the same on different platforms unlike most of other Java aspects. Two main areas of difference are thread scheduling and thread priorities.
In S60 3rd Edition FP 2 onwards, Java threading model has been changed to use native threading. This means that each Java thread maps to the native thread. Previously all Java threads ran in single native thread (green threads). Threading change is transparent for MIDlets and does not cause any issues for correctly implemented MIDlets.
For thread handling in MIDlets:
The choice of which thread the thread.notify
method
wakes is arbitrary.
Java VM process can have maximum of 128 threads. VM implementation itself has some threads so the number of available threads to MIDlet may vary.
Thread priorities should not be modified because this may not result in performance gains and may have adverse effects.
Thread.setPriority()
S60 implementation uses native threading model where a Java thread maps to a native thread. Table below lists how Java thread priorities are mapped to native thread priorities.
Java thread priority |
Symbian OS thread priority |
---|---|
1...4 |
|
5...8 |
|
9...12 |
|
Thread priorities should not be modified. There are often better ways to solve timing or resource acquisition problems. Especially if high thread priorities are used then developer should understand the ramifications from whole Symbian OS point of view. Scheduler is not fair so a higher priority Java thread gets all the CPU it needs, which may starve lower priority Java threads.
Changing thread priority rarely gives any performance gains and it may have actually reversed effects.
Thread.yield()
Do not use the thread.yield
method because it can
lead to busy loop-like behavior as yielding thread can be scheduled to run
immediately again if there are no other eligible threads to be run.
In S60, wait-notify
is always the better alternative
to a Thread.yield
loop, when a condition needs to be
acted on.
Thread.sleep()
Do not use the thread.sleep
method because it can
lead to busy loop-like behaviour that can affect the battery life and other
applications.
Wait-notify should also be always preferred over Thread.sleep
looping,
as Thread.sleep
does not react to events immediately
(even a small sleep value takes at least that long to react).