B4J Question Regarding jServer's AddHandler

Bruce Axtens

Active Member
Licensed User
Longtime User
AddHandler (Path As String, Class As String, SingleThreadHandler As Boolean)

Maps a handler class.
Path - The handler will be mapped to the following path. You can use wildcards in the path.
Class - The class name (string).
SingleThreadHandler - Whether this handler should always run in the main thread.

What is the difference between SingleThread and whatever it's opposite is, in the context of jServer? If some handlers are marked True and some False, does that negatively affect anything?
 

Bruce Axtens

Active Member
Licensed User
Longtime User
Threading

When you add a handler you need to specify whether you want it to be a single threaded handler or a multithreaded handler. By default you should use multithreaded handlers (third parameter should be false).

A multithreaded handler means that your handler code will be run by a thread from the server threads pool. Multiple instances of the same handler and other handlers can be executed in the same time. As long as you don't access any global variable out of the current handler instance you should be safe.

A single threaded handler will always be executed by the main thread. This means that if there are multiple requests they will be queued and executed one by one. Single threaded handlers can be useful in many cases. Some examples:
- A handler that accesses a SQLite database, which is less suitable for concurrent connections.
- A handler that writes to a specific file.
- A handler that sends a job to the printer

Note that when you run your code in Debug mode the handlers will always run in the main thread.

Found in https://www.b4x.com/android/forum/threads/server-building-web-servers-with-b4j.37172/#content
 
Upvote 0
Top