B4A Library ThreadUtils

Hello all,

This is my first published B4A library…

For my need I begin library with, I think, usefull class associated with threading.
I'm not easy with java, so this library is entirely write with B4A.
It is based on excellent Threading library from agraham.

For now there is only one class inside this library : ParallelLoop

With ParallelLoop you can distribute For and ForEach loop in different thread.

To use it, you must define Sub with code for one index (For) or one item (ForEach) :

B4X:
Private Sub FcnName(Id as Int)
    "Code for this Id"
End Sub

B4X:
Private Sub FcnName(Item as ItemType)
    "Code for this Item"
End Sub

Then you can call ParallelFor methods to launch loop.

Some warnings :

Thread is relatively big object and need some time to initialize. If possible, I suggest declaring this class at high level (Process_Globals) and reusing it. Mechanisms to avoid collision with multiple calls are included.

In the Initialize sub, you must give maximum number of thread, so that is maximum parallel execution of your loop. From my first test, giving number of CPU core get the best result. If someone can give me code to detect number of core, I include it in future release…

Multithreading is not panacea. To obtain gain, compare to mono-thread, each loop must consume processor time. Due to thread overhead, you obtain bad performance if you use it for lot of loop of very quick treatment. But if you use it with adapted code, you can obtain real improvement. I try it with intensive calculation and obtain improvement of just more than 3 with quad-core (Galaxy Note 10.1).

If you want to follow core usage, I suggest using “Micro CPU Monitor” from The Big Byte.
 

Attachments

  • ThreadUtils.zip
    5.8 KB · Views: 233
Top