If the task has been started, the isStarted () method returns true:
Public synchronized boolean isStarted (){
Return started;
}
If the task has been completed, the isCompleted () method returns true:
Public synchronized boolean isCompleted (){
Return counter = 100;
}
If the task is running, the isRunning () method returns true:
Public synchronized boolean isRunning (){
Return running;
}
The SetRunning () method is called by start. jsp or stop. jsp. When the running parameter is true. The SetRunning () method also marks the task as "started ". Calling setRunning (false) indicates that the run () method is required to stop execution.
Public synchronized void setRunning (boolean running ){
This. running = running;
If (running)
Started = true;
}
After the task is completed, call the getResult () method to return the calculation result. If the task has not been completed, it returns null:
Public synchronized Object getResult (){
If (isCompleted ())
Return new Integer (sum );
Else
Return null;
}
When running is marked as true and completed is marked as false, the run () method calls work (). In practical applications, the run () method may need to execute complicated SQL queries, parse large XML documents, or call the EJB method that consumes a lot of CPU time. Note that "heavy tasks" may be executed on remote servers. The JSP page of the report result has two options: Wait for the task to end, or use a progress bar.
Public void run (){
Try {
SetRunning (true );
While (isRunning ()&&! IsCompleted ())
Work ();
} Finally {
SetRunning (false );
}
}