Author: FavoYang Email: favoyang@yahoo.com welcome
Keywords
Abstract:
This article is a continuation of the article "progress bar and thread-based model of j2_m3-based architecture" (for short, the original article is short. I have not read it before. I suggest you take a look at it ).
The shortcomings of the thread model used in the original article are discussed, and a new improvement method is proposed for her shortcomings, and the implementation after improvement is given. The UI part in the original text is flexible and not changed.
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:
How the foreground UI interacts with the background thread
The model in the original text is a foreground ProgressGaugeUI model unrelated to the background thread. In this way, the complexity of communication is reduced to the maximum extent. It is actually a single-direction model (Communication from BackgroundTask to PGUI ). According to the requirements of this mode, the programmer is obligated to regularly check the running status of the PGUI at the front-end of the training in the Override BackgroundTask runTask () method and reflect the situation accordingly. In this mode, I fully believe that the background thread has handed over the right to respond to the user's cancel command to the background thread. If the background thread is in trouble and does not respond (for example, accessing a very expensive network connection ), at this time, it is useless for the user to try cancel, and the program will temporarily deadlock until the background thread has time to check the foreground status. In addition, in the actual situation, it is a problem to determine when to query the query frequency. Too much such code in the code segment will affect your understanding of the normal process.
The following sequence diagram shows the specific process:
We need a method to force the Task to end. This method is provided by the background thread and called cancel (). Of course, there is no way to force the thread to end immediately (once, it was canceled due to security issues ). Therefore, the cancel () method often forces the runTask to be interrupted by a closed Resource (a connection, a stream, etc.). runTask is obligated to capture such exceptions and exit immediately according to its own conventions. Let's take a look at the process of this method.
Obviously, the key is that the foreground thread calls back the background thread to solve the problem. But the new problem arises. This forces us to closely couple the foreground and background threads (because callback is required ). Can we implement callback and avoid the Close coupling between the foreground UI and background threads?
Reduce Coupling Degree through Cancelable Interface
Fortunately, we can use interfaces to achieve this.
The previous model is as follows:
To reduce coupling, we create an interface