J2's progress bar and threading model
Author: FavoYang Email: favoyang@yahoo.com welcome
Keyworld
Abstract:
This article describes how to create a convenient thread model, which consists of the progress bar UI at the front end and background thread at the background.
Copyright:
This article is also published on www.j2medev.com and my Blog (blog.csdn.net/alikeboy). If you need to reprint it, there are three ways: 1) contact me and I agree to it; 2) I have a cooperation agreement with www.j2medev.com for reprinting articles. 3) Use Rss to aggregate my Blog. In addition, you must forward the full text (including the header of the article.
Body:
Solved problems
UI operations run in an independent thread in the UI System of J2EE. In api doc, programmers are often required to return interface methods immediately. That is, non-blocking. You must enable an independent thread to complete your custom complicated work, such as network connection and other io operations that may be blocked. If the new thread does not communicate with the user and tells the user that the thread is working, it will be very unfriendly. The user may perform other operations to disrupt the normal operation of the program. A simple method is to provide a progress bar, so that the user will be willing to wait for the last meeting until the program runs out of the results. To free programmers from the communication between the front-end progress bar and background threads and concentrate on background thread development, it is necessary to design a progress bar thread model.
It should be noted that the progress bar has multiple forms:
A. animated progress bar, indicating that the program is running (self-maintained)
B. Progress bars in interactive incremental form. The background thread continuously changes the progress bar status during the running of the program by calling the corresponding method of the progress bar.
C. The progress bar should be displayed in a flexible manner and should not be fixed.
D. The progress bar object must be reused.
There are also several situations for progress reconciliation and background thread communication:
A. Only the progress bar is painted on the screen. After the background task is completed, the background thread jumps to the success screen.
B. For canceled tasks, you can click the progress bar to try the cancel task. The background task should be canceled as soon as possible and jump to the failed screen.
C. Users can only wait patiently for tasks that cannot be redirected.
D. If the background thread fails to run, jump to the failed screen.
Progress bar design (front-end)
To achieve the diversity of the progress bar, first abstract an interface:
ProgressObserver. java
Package com. favo. ui;
Import javax. microedition. lcdui. Display;
/**
* @ Author Favo
*
* This is a progress bar observer modeled on Smart Ticket. The advantage of this model is that
* 1. Low coupling. You can use Form and Canvas to implement this interface.
* 2. Support for interruptible tasks because the background thread cannot be forcibly interrupted,