B4J Question Multithreading on Windows/Linux using something similar to System.Threading.Thread and Thread.Start?

june8

Member
This question hasn't really been answered from what I can find in the forums. There is no facility for multithreading in B4A except for some classes (eg HTTP), as Android (or B4A) doesn't support it. Everything runs in async queues, like in Javascript. You can't "spawn" a new thread for a particular instance of your class.

But I was wondering how do we get true threading in B4J? The equivalent of .Net's System.Threading.Thread class and Thread.Start? I've found this feature immensely useful in Windows and was hoping the same can be done in B4J.

In other posts, Erel asked for more information, here is an example.

I have B4A project that uses Wait-For with HTTP Get calls (which use a built-in queuing system), but then I need to do some processing on separate threads, each which calls more HTTP Gets. Obviously this can't be done in Android or B4A (because they queue up, rather than multithread). But is real Threading possible with B4J programs on Windows & Linux deployments? The underlying oeprating systems are capable of it (because we do threading in .NET), but how to do in B4X, as I want to convert the program over to work on B4J?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm afraid to say that everything you wrote here is wrong.

Http requests are already multithreaded. Here is an example that sends 1000 concurrent requests:
B4X:
For Each link As String In Links 'Links is a List with 1000 urls
 MakeRequest(link)
Next

Private Sub MakeRequest(Link as String)
Dim j As HttpJob
j.Initialize("", Me)
j.Download(link)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
    'do something
End If
j.Release
 
Upvote 0

june8

Member
That may be the case for the built-in library HttpJob, but how can we get multiple threads running for our own classes in say Windows/Linux with B4J? Somebody already asked this before and was referred to the 3rd party Threading library for B4X, which was not recommended by Erel. Simply saying I'm wrong without showing how to do it is disingenuous really. I already said I understand the HttpJob is multithreaded, didn't I?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Most libraries implement asynchronism where it is appropriate so threading is not normally required, However if you want to get dirty my Threading library works on B4A and B4J as it is pure Java. It was useful in the early days of B4X but since then Erel has reduced the need for it by adding asynchronism where appropriate.
 
Upvote 0

june8

Member
Thanks agraham. My previous comment is blocked, so I want to clarify my requirement to see if it can be done with pure B4X.

Let's say I have a class that fetches a HTTP request and does some heavy processing on it, including calling more HTTP requests to get extra information. In DotNet I create new threads and let them run until they return with ALL the processing done, including the GETs. Then I merge the results. It seems with B4X the Wait-For can make many instances of asynch calls to HTTP GET, but then is serialised at the calling routine when processing each of those results, one by one, which is basically Sync/Serial processing of each HTTP call. In DotNet the whole GET and custom processing is offloaded to the thread, which returns the results to the main caller. I can create as many threads as I like, not limited to # of CPU's. I am not sure if I can do this with built-in B4X, except using your Threading library, which I am hesitant to do.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Simply saying I'm wrong without showing how to do it is disingenuous really. I already said I understand the HttpJob is multithreaded, didn't I?
You wrote:
but then I need to do some processing on separate threads, each which calls more HTTP Gets. Obviously this can't be done in Android or B4A (because they queue up, rather than multithread).
This is wrong. You can make as many concurrent requests as you need. They do not queue up.

but then is serialised at the calling routine when processing each of those results, one by one, which is basically Sync/Serial processing of each HTTP call.
Not exactly. The heavy part is multithreaded. It is only your B4X code that runs on the main thread. The actual network communication runs on secondary threads.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Wait-For can make many instances of asynch calls to HTTP GET, but then is serialised at the calling routine when processing each of those results, one by one, which is basically Sync/Serial processing of each HTTP call.
This is essentially true if you are doing a significant amount of processing after each WaitFor returns as all these will run on the main thread. On the other hand using threads does not mean that it is any more efficient than performing the work on the main thread. The main benefit of offloading work from the main thread would be to keep the UI responsive.
 
Upvote 0

june8

Member
Thanks, at the moment its very slow to finish all requests on B4A, but I am going to try it on B4J and in Windows/Linux to see if its as fast as what I am getting with the "other" big name tool, I can't name here. That one finishes almost instantly due to real threading, it would be good if we could take advantage of the Windows/Linux OS threading to do the same there.
 
Upvote 0

june8

Member
Ok I think I have miscommunicated this. I know network requests (and some built it libraries) are already multithreaded, let's just ignore that one for now. I wanted to kick off batches of Threads of my custom class eg "clsDoStuff", regardless of what they do, and catch them when finished, topping up the thread pool with new jobs when some have finished. eg. Have 5 threads going at a time and doing something when all finished. Threading like this has been around forever, I've used it in PHP and ??.NET. Anyhow, I will give it a try in B4J. Thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Let me explain, threading is of course supported by Android, iOS and desktops. B4X makes heavy usage of the OS threading features.

Such questions appear from time to time and in 99% of the cases the developer eagerness to use the Threading library turns out to be a mistake. B4X, as a RAD tool, is built in such way that multithreaded related features are implemented inside the libraries.

For a more fruitful discussion you need to tell us what happens inside clsDoStuff.

And make sure to always test performance in release mode.
 
Upvote 0

june8

Member
Thank Erel. Since we're getting technical, does the new Wait..For construct create concurrent threads for B4J in Windows/Linux. ie. as a built-in feature of the B4X language, is it implemented as threads or an async queue? There is a big difference, as one is basically serialised and the other is parallel. I want to be able to parallel process on threads in Windows/Linux environment, where its genuinely a multithreaded O/S.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Wait..For construct create concurrent threads for B4J in Windows/Linux. ie. as a built-in feature of the B4X language, is it implemented as threads or an async queue?
 
Upvote 0
Top