A mechanism for controlling threads in a Java application while avoiding
the unsafe conditions inherent in the use of existing java.lang.Thread
methods. In one embodiment, a first class is defined for handling threads
in an application. The first class uses a target variable to indicate
whether a thread should continue to run, or whether it should be stopped.
This first class provides a start( ) method to set up the target variable,
a stop( ) method to set the target variable to indicate that the thread
should be stopped, and an abstract run( ) method. The functionality of the
run( ) method is provided by one or more additional classes which extend
the first class. The additional classes override the abstract run( )
method and define the tasks to be performed by threaded objects
instantiated from these classes. When a thread needs to be stopped, the
corresponding target variable is set to indicate that it should be
stopped. The thread periodically checks the target variable and, when the
target variable is set to indicate that the thread should be stopped, the
thread executes one or more instructions that cause execution of the
thread to complete and to exit normally.