Programming Guide:xppguide

The two sides of a Thread object Foundation

It is not obvious from the implementation of a user-defined Thread class which methods are executed in which thread, unless you know the purpose of the three pre-defined methods :atStart(), :execute() and :atEnd(). Code that must run in the thread represented by the Thread object must be implemented in these methods. However, a Thread object has methods which must be called from another thread. This makes it necessary to distinguish two kinds of methods from a logical point of view which is most easily done using the terms Client and Server.

You may think of a Server being the black box in the computer room down the hallway and a Client being a similar thing sitting on your desk. This view is correct in the context of hardware. But in the field of software, Client/Server is a concept not necessarily related to physical entities. Instead, different parts of a software can play the Client/Server roles when the program is split into multiple threads. Assume a browser that displays a monthly consumer statistic. If a thread calculates consolidated data, a user can start viewing the results for the first months already while the thread still calculates the rest of the year. This would need two threads, one for the display and one for the calculation:

In this scenario, thread A uses data created by thread B, or, in other words, thread A is being served with data by thread B. Hence, it is legitimate to state that thread A is a Client of thread B while thread B is the Server thread. Looking at this from the Thread object's point of view we can conclude that :start() is a method which must be called in the Client thread because it causes the Server thread to run. Since a thread cannot be started twice while it is running, the :start() method is restricted to a Client thread, it is a client-side method.

In contrast, the :execute() method always runs in the thread represented by the Thread object. It is impossible that this method runs in another thread, it is a server-side method. We can, therefore, distinguish two sides of a Thread object by the thread in which methods are executed: client-side and server-side. The former consists of methods which cannot be executed in the thread represented by the object, while the latter is a group of methods that are always executed in this particular thread. There is, however, a third group of methods which can be executed in any thread. They define settings about how a thread is executed. This covers thread priority and timing issues.

Method groups of the Thread class
Client side Server side Settings
:new() :atStart() :setInterval()
:init() :execute() :setPriority()
:setStartTime() :atEnd()
:start() :quit()
:synchronize()

The distinction beween client-method and server-method of a Thread object is useful since both terms help to structure a multi-threaded program easily. Server-methods perform tasks independently of the rest of a program (they serve something else), while client-methods create threads and control the program flow between them.

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.