The first chapter, the main thread and the child thread

Source: Internet
Author: User

the first chapter, the main thread and the child thread
But the moment we open the FBReader program, the code immediately creates a child thread. After that, the main thread is responsible for displaying a progress bar on the screen, while the child thread starts reading the epub file in the background.

PS: The progress bar looks like this, but as the loading speed is fast, the progress bar is flashing. This image is intercepted in a single-step debugging environment.

In this chapter, we will describe in detail how the program creates child threads and how to control the display and disappearance of the progress bar.
The core class involved in this chapter is the Uiutil class.


FBReader default first enters the FBReader class, which is set in Androidmanifes.xml.

OnCreate methods in the Zlandroidactivity classThe OnCreate method of the FBReader class first triggers the OnCreate method in the parent class Zlandroidactivity class of the FBReader class.

A series of initialization work is done in this method:
102 Line: Set Program to full screen
107 Line: Set the interface of the program, this interface is the Viewzlandroidwidget class. We will analyze the OnDraw method of this class in detail in the tenth chapter, "Epub File processing-display".
110 Rows: Call the Setactivity method of the Zlandroidlibrary class, assigning a value to the myactivity variable inside the Zlandroidlibrary class (please remember this method, we will use this variable immediately)
114 Rows: Call the CreateApplication method of the FBReader class, returning a Fbreaderapp class
Line 117: Call the Initwindow method in the subclass Fbreaderapp of the Fbreaderapp class, which will be responsible for creating the child thread and displaying the progress bar on the main thread

Initwindow method of Fbreaderapp class, Zlandroidapplicationwindow class Wait methodThe Initwindow method of the Fbreaderapp class calls the Wait method of the Zlapplication class. The wait method of the Zlapplication class will eventually call the Zlandroidapplicationwindow class wait method
Keep in mind the Runnable class defined in the Initwindow method, which is the code that will then read the Epub file to run in the child thread.

Remember the Setactivity method of Zlandroidlibrary class you just wanted to remember? At the time, this method assigns a value to the myactivity variable of the Zlandroidlibrary class. Now the wait method of the Zlandroidlibrary class will judge this property, such as NULL, to run the code that reads the epub file directly in the main thread (this may cause the UI in the main thread to stall), or if this property is not NULL, The Wai method of the Uiutil class is called, which is divided into two thread handlers

Uiutil class Wait MethodIn the wait method in the Uiutil class, we can see that the code opens a new child thread (78 rows), and the code running in the child thread is the contents of the Runnable class defined in the Initwindow method of the Fbreaderapp class of the Zlapplication class. At the same time, the main thread calls the ProgressBar class's show method to show a progress bar (we'll detail how to get the words to be displayed in the progress bar in the next chapter).


About the progress bar, there are two parts need to be described in detail: the first is how to get the progress bar to disappear, the second is how to get the progress bar to display the text.

the disappearance of the progress barLet's start by describing how to make ProgressBar disappear.
The way to make the progress bar display is simple, just call the show method of the ProgressDialog class.

The Ourprogresshandler variable points to the handler class, and once you receive the "Notification" of the child thread, the progress bar disappears.

Please note here that the wait method for the Uiutil class takes into account multiple background tasks, how the progress bar appears and disappears. Multiple background tasks, it must be the case that all tasks are completed in order to let the progress bar disappear, then how is the code implemented?

The code first saves all background tasks in the wait method of the Uiutil class with the Ourtaskqueue property

Then, in the handler class, determine if the ourtaskqueue is empty, and the progress bar disappears when it is empty

Also, because both the child thread and the handler class are operating Ourtaskqueue, the wait method and the Notify method must be used to ensure that the entire variable is thread safe

text to display on the progress barNext, I'll show you how to get the text ProgressBar to display
The text that appears on the progress bar is the message variable that is read from the resource file.

The location of the resource files is in assets/resourses application and zlibrary two folders.

The two folders store versions of various languages, where the FBReader program defaults to Uk.xml, and the code eventually displays the contents of the Zh.xml based on the language settings of the phone.

The text we are looking for is displayed in ProgressBar the Value property of the Waitmesssage node under the Loadingbook node of the dialog node in the Application Folder Zh.xml file.

The code specifically parses the Zh.xml file to get the Value property under the specified node, which we'll cover in the next chapter.

The first chapter, the main thread and the child thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.