B4J Question About Threads and Modules.

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
Erel somewhere says that the code into code modules is executed in the main thread of the application.
My question is: where is executed the code of the code modules called from a thread of a multithreaded application? In the main thread or in the thread called it?

Thank you in advance!
 

billzhan

Active Member
Licensed User
Longtime User
Guess it's about b4j server.


See thread and Erel's answers (in post #4)
[WebApp] Chatroom - Threads, Sessions and Server events

The code in code modules will run in the main thread if we call it with CallSubDelayed.

...

4. This is very important point. See the "threads" section in the first post. It is only relevant to server (or WebApps) projects. Each class instance runs in its own thread. If you call ChatShared.NewConnection then this sub will be executed in the class instance thread.
If you call CallSubDelayed then the target thread will be used to execute the code. In the case of code modules it is always the main thread.


https://www.b4x.com/android/forum/threads/threadsafe-map
Unless it is explicitly written that the event runs on a different thread then the event will run on the main thread. This is the case with ServerSocket, Socket and AsyncStreams events.

They do handle the communication with background threads however the events are delegated to the main thread.
 
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
Thank you for your reply billzhan!
Yes, it's about B4J Server!

"Unless it is explicitly written that the event runs on a different thread"

How could I write that the event runs on a different thread?

Any help is highly appreciated!
 
Upvote 0
Top