CN109388505B - A kind of asynchronous multitask sequence loading method based on Android - Google Patents
A kind of asynchronous multitask sequence loading method based on Android Download PDFInfo
- Publication number
- CN109388505B CN109388505B CN201811130892.1A CN201811130892A CN109388505B CN 109388505 B CN109388505 B CN 109388505B CN 201811130892 A CN201811130892 A CN 201811130892A CN 109388505 B CN109388505 B CN 109388505B
- Authority
- CN
- China
- Prior art keywords
- asynchronous task
- asynchronous
- asynctask
- class
- task
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/54—Interprogram communication
- G06F9/546—Message passing systems or structures, e.g. queues
Abstract
The invention discloses a kind of asynchronous multitask sequence loading method based on Android, its step includes: to create several AsyncTask asynchronous task classes, is that specific asynchronous task content is arranged in the doInBackgroud () method built in each AsyncTask asynchronous task class;The creation circulation body method in UI main thread, the circulation body method is for gradually executing AsyncTask asynchronous task, until all asynchronous tasks are finished.It can be executed according to the design sequence of developer using method provided by the present application when UI main thread needs asynchronous load mass data, it is compared to the prior art, simple for structure, efficient, easy to maintain, it is avoided that unnecessary mistake occurs in program.
Description
Technical field
The present invention relates to field of communication technology more particularly to a kind of asynchronous multitask sequence load sides based on Android
Method.
Background technique
Realize that there are two types of modes for asynchronous task mechanism in Android: Handler and AsyncTask.Handler mode
It needs for one new thread of each task creation, passes through Handler example after the completion of task to UI thread and send message, it is complete
At the update at interface, this mode is finer for the control of whole process, but is also imperfect, such as code is with respect to swelling
It is swollen, it when multiple tasks are performed simultaneously, is not easy to be accurately controlled thread, AsyncTask class makes to create asynchronous task change
It obtains simpler, it is no longer necessary to write mission thread and identical task, the version after 2.3 can be completed in Handler example
Originally it can support parallel and serial.The shortcomings that AsyncTask is that the example that it is created can only be executed once, and is otherwise repeatedly called
When will will appear exception.The case where asynchronous multitask is often used in actual development, such as load data containing picture
List, in order to promote the experience of user, generally can all use asynchronous load since the load of picture is than relatively time-consuming.Above 2 kinds of sides
The problem of formula is in single task does not protrude, but its drawback will highlight in multitask, it is difficult to reach according to column
The purpose that table sequencing executes.
Summary of the invention
Mirror is with this, the purpose of the present invention is to provide a kind of asynchronous multitask sequence loading method based on Android, with
At least solve problem above.
A kind of asynchronous multitask sequence loading method based on Android, comprising:
Several AsyncTask asynchronous task classes are created, are built in each AsyncTask asynchronous task class
Specific asynchronous task content is arranged in doInBackgroud method;
The creation circulation body method in UI main thread, the circulation body method is for gradually executing AsyncTask asynchronous
Business, until all asynchronous tasks are finished.
Further, the circulation body method passes through monitoring when gradually executing AsyncTask asynchronous task
Method return value built in AsyncTask asynchronous task class judges whether AsyncTask asynchronous task executes completion.
Further, when gradually executing AsyncTask asynchronous task, the AsyncTask asynchronous task of return value calls
Its onPostExecute method completes the load of resource in UI main thread.
Further, it is arranged specifically in creation AsyncTask asynchronous task class and for each AsyncTask asynchronous task class
After asynchronous task content,
Total data needed for asynchronous task being executed is added to one by one in an array;
When the circulation body method executes loop body every time, an asynchronous task is taken out in order from the array;
When data are taken out successfully, the asynchronous task is executed;Otherwise it represents asynchronous task sequential queue to be finished, termination follows
Ring.
Further, total data needed for executing asynchronous task using the sparse array storage of SparseIntArray.
Further, when data are taken out successfully, the asynchronous task is executed, comprising:
The doInBackgroud method of AsyncTask asynchronous task class is called to execute asynchronous in backstage creation new thread
Business;
Asynchronous task calls the onPostExecute method of AsyncTask asynchronous task class in UI main line after the completion of executing
The load of journey completion resource.
Further, QueueAsyncTask class is created, described in the sparse array of the SparseIntArray is used as
The variable of QueueAsyncTask class is for storing asynchronous task sequential queue, while QueueAsyncTask class is stated respectively
DoInBackgroud interface and onPostExecute interface, doInBackgroud interface is for realizing different to AsyncTask
Walk the calling of the doInBackgroud method of task class;OnPostExecute interface is for realizing to AsyncTask asynchronous
The calling of the doInBackgroud method for class of being engaged in.
Compared with prior art, the beneficial effects of the present invention are:
A kind of asynchronous multitask sequence loading method based on Android provided by the invention, by UI main thread
Design cycle body realizes that asynchronous multitask according to the effect sequentially executed desired by developer, is solved and needed in exploitation
The problem of asynchronous load mass data, structure is more succinct compared to the prior art, efficient and easy to maintain, is avoided that program goes out
Existing unnecessary mistake.
Detailed description of the invention
To describe the technical solutions in the embodiments of the present invention more clearly, make required in being described below to embodiment
Attached drawing is briefly described, it should be apparent that, the accompanying drawings in the following description is only the preferred embodiment of the present invention, for
For those of ordinary skill in the art, without creative efforts, it can also be obtained according to these attached drawings other
Attached drawing.
Fig. 1 is the overall system architecture schematic diagram of the embodiment of the present invention.
Fig. 2 is the asynchronous task class creation of the embodiment of the present invention and flow diagram after accomplishing the setting up.
Specific embodiment
It should be mentioned that some exemplary embodiments are described as before exemplary embodiment is discussed in greater detail
The processing or method described as flow chart.Although operations are described as the processing of sequence by flow chart, therein to be permitted
Multioperation can be implemented concurrently, concomitantly or simultaneously.In addition, the sequence of operations can be rearranged.When it
The processing can be terminated when operation completion, it is also possible to have the additional step being not included in attached drawing.The processing
It can correspond to method, function, regulation, subroutine, subprogram etc..
The principle and features of the present invention will be described below with reference to the accompanying drawings, and cited embodiment is served only for explaining this hair
It is bright, it is not intended to limit the scope of the present invention.Specific structure and function details disclosed herein are only representative, and
It is the purpose of exemplary embodiment for describing the present invention.But the present invention can be by many alternative forms come specific real
It is existing, and be not interpreted as being limited only by the embodiments set forth herein.
Referring to Fig.1, the present invention provides a kind of asynchronous multitask sequence loading method based on Android, the method packet
It includes:
Step S1 creates several AsyncTask asynchronous task classes, is built in each AsyncTask asynchronous task class
Specific asynchronous task content is arranged in doInBackgroud method.
Work as application program launching, Android will create a main thread, and main thread is responsible for UI component Distribution Events, is drawn
Event processed, in main thread, the UI component of application program and android system is interacted, so main thread is also referred to as UI
Main thread.Due to only having UI main thread that could be updated operation to UI, other threads can not direct operation UI, such benefit
It is the stability and accuracy that ensure that UI, avoiding multiple threads, operation UI causes confusion simultaneously, but cannot be by all tasks all
It is put into UI main thread and realizes, such as the time-consuming operations such as network operation, large capacity file reading, be otherwise likely to cause subsequent
The obstruction of task, when plugged between it is too long, android system can dish out ANR mistake, for requiring the application program of stability to answer
This is avoided the generation of such case as possible, it is therefore desirable to consuming task is put into non-main thread and go to execute.AsyncTask is asynchronous
Task class is a class dedicated for handling asynchronous task.By such, the logical of UI main thread and background thread may be implemented
News, background thread execute the asynchronous task of developer's setting, and return result to UI main thread, avoid Android application
Single-threaded model and dish out ANR the case where.
Step S2, the creation circulation body method in UI main thread, the circulation body method is for gradually executing AsyncTask
Asynchronous task, until all asynchronous tasks are finished.
In step S2, the circulation body method passes through monitoring when gradually executing AsyncTask asynchronous task
Method return value built in AsyncTask asynchronous task class judges whether AsyncTask asynchronous task executes completion.
Main built-in four methods of AsyncTask asynchronous task class: onPreExecute (), doInBackground (),
OnProgressUpdate (), onPostExecute (), wherein the time-consuming operation completed required for asynchronous task is written in
In the method body of doInBackground (), system can open a work thread to execute this in the thread pool on backstage
A method.When an asynchronous task is finished, system can call onPostExecute () method automatically, and will
The value that doInBackground () method returns is passed in this method, to be updated accordingly to UI, by monitoring this method
Return value, judge whether asynchronous task executes completion, when the asynchronous task currently monitored is completed, execute next asynchronous
Business, until asynchronous task is all finished.
On the basis of the above embodiments, in order to allow multiple asynchronous tasks to execute according to the imagination sequence of developer,
To the structure of compact code, that improves code can be readability, at the same when being updated to UI to user with good sensory experience, need
Will to asynchronous task execution sequence take method to limit.In reference Fig. 2, step S1, in creation AsyncTask asynchronous
Class of being engaged in and after specific asynchronous task content is set for each AsyncTask asynchronous task class, further includes:
Step S11, total data needed for executing asynchronous task are added to one by one in an array.
In step S11, total data needed for executing asynchronous task is stored in the sparse number of SparseIntArray one by one
In group.When contents value most in array is all not used by (or being all zero), only least a portion of space is used in array,
Therefore the waste of memory headroom is caused, or even will cause memory spilling, therefore in order to achieve the effect that save memory headroom, simultaneously
The asynchronous task data stored in array are not influenced, using the sparse array of SparseIntArray as storage asynchronous task number
According to set, the sparse array of SparseIntArray is divided into two parts, a part for record the columns of former array, line number and
The number that element uses, it is binary search function that second part, which records the position of element and content, core in former array, uses number
Group data structure maps to save, and required data object is then found by binary search, does not depend on external object mapping, can
To effectively improve memory efficient.
Step S12, when the circulation body method executes loop body every time, taken out in order from the array one it is asynchronous
Task.
In step S12, since each asynchronous task is stored in array, by unique array index to asynchronous task
Institute storage position indicated, can be according to loading sequence needed for developer when by the storage of asynchronous task data into array
Asynchronous task is stored one by one, is taken out by array index sequence to execute, to realize asynchronous task by suitable when taking out
Sequence load.It can also realize that discrete array data is read by design cycle body circulation condition according to exploitation demand simultaneously
It takes, asynchronous task data can not have to be stored according to loading sequence in array at this time.
Step S13 when data are taken out successfully, executes the asynchronous task;Otherwise asynchronous task sequential queue is represented to have executed
Finish, terminates circulation.
In step s 13, after data are taken out successfully, the asynchronous task is executed, comprising:
DoInBackgroud () method of AsyncTask asynchronous task class is called to execute in backstage creation new thread asynchronous
Task.
Asynchronous task calls onPostExecute () method of AsyncTask asynchronous task class in UI master after the completion of executing
The load of thread completion resource.
In an alternate embodiment of the present invention where, in order to guarantee that task queue is serially executed by the sequence according to customization,
The degree of coupling between application program modules is reduced simultaneously, to improve the scalability of application program, while program being avoided to exist
Occur unnecessary mistake in operational process, guarantee its even running, QueueAsyncTask class is created, described
Stated in QueueAsyncTask class the sparse array of SparseIntArray with for storing asynchronous task sequential queue, while
DoInBackgroud () interface and onPostExecute () interface are stated in the QueueAsyncTask class respectively,
DoInBackgroud () interface for realizing doInBackgroud () method to AsyncTask asynchronous task class calling;
OnPostExecute () interface for realizing doInBackgroud () method to AsyncTask asynchronous task class calling,
In the implementation procedure of asynchronous serial multitask, necessary to the QueueAsyncTask class is only exposed to outside
DoInBackgroud () interface and onPostExecute () interface, so that the code degree of coupling reduces, it is easy to maintain;
It can also be according to exploitation other interfaces of statement of need to call in AsyncTask asynchronous task class in QueueAsyncTask class
Correlation technique, scalability are strong.
It should be noted that the present invention can be carried out in the assembly of software and/or software and hardware, for example, can adopt
With specific integrated circuit (ASIC), general purpose computer or any other realized similar to hardware device.In one embodiment
In, software program of the invention can be executed to implement the above steps or functions by processor.Similarly, of the invention
Software program (including relevant data structure) can be stored in computer readable recording medium, for example, RAM memory,
Magnetic or optical driver or floppy disc and similar devices.In addition, some of the steps or functions of the present invention may be implemented in hardware, example
Such as, as the circuit cooperated with processor thereby executing each step or function.
The foregoing is merely presently preferred embodiments of the present invention, is not intended to limit the invention, it is all in spirit of the invention and
Within principle, any modification, equivalent replacement, improvement and so on be should all be included in the protection scope of the present invention.
Claims (4)
1. a kind of asynchronous multitask sequence loading method based on Android, which comprises the following steps:
Several AsyncTask asynchronous task classes are created, are built in each AsyncTask asynchronous task class
Specific asynchronous task content is arranged in doInBackgroud method;
Total data needed for asynchronous task being executed is added to one by one in an array, uses the sparse number of SparseIntArray
Total data needed for group storage executes asynchronous task, specifically includes: creation QueueAsyncTask class, described
The sparse array of SparseIntArray is used to store asynchronous task sequential queue as the variable of the QueueAsyncTask class,
QueueAsyncTask class states doInBackgroud interface and onPostExecute interface respectively simultaneously,
DoInBackgroud interface for realizing the doInBackgroud method to AsyncTask asynchronous task class calling;
OnPostExecute interface for realizing the doInBackgroud method to AsyncTask asynchronous task class calling;
The creation circulation body method in UI main thread, the circulation body method is for gradually executing AsyncTask asynchronous task, directly
It is finished to all asynchronous tasks;
When the circulation body method executes loop body every time, an asynchronous task is taken out in order from the array;
When data are taken out successfully, the asynchronous task is executed;Otherwise it represents asynchronous task sequential queue to be finished, terminates circulation.
2. a kind of asynchronous multitask sequence loading method based on Android according to claim 1, which is characterized in that
The circulation body method is when gradually executing AsyncTask asynchronous task, by monitoring built in AsyncTask asynchronous task class
Method return value judges whether AsyncTask asynchronous task executes completion.
3. a kind of asynchronous multitask sequence loading method based on Android according to claim 1, which is characterized in that
When gradually executing AsyncTask asynchronous task, there is the AsyncTask asynchronous task of return value to call its side onPostExecute
Method completes the load of resource in UI main thread.
4. a kind of asynchronous multitask sequence loading method based on Android according to claim 1, which is characterized in that
When data are taken out successfully, the asynchronous task is executed, comprising:
The doInBackgroud method of AsyncTask asynchronous task class is called to execute asynchronous task in backstage creation new thread;
Asynchronous task calls the onPostExecute method of AsyncTask asynchronous task class complete in UI main thread after the completion of executing
At the load of resource.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201811130892.1A CN109388505B (en) | 2018-09-27 | 2018-09-27 | A kind of asynchronous multitask sequence loading method based on Android |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201811130892.1A CN109388505B (en) | 2018-09-27 | 2018-09-27 | A kind of asynchronous multitask sequence loading method based on Android |
Publications (2)
Publication Number | Publication Date |
---|---|
CN109388505A CN109388505A (en) | 2019-02-26 |
CN109388505B true CN109388505B (en) | 2019-11-15 |
Family
ID=65419063
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201811130892.1A Active CN109388505B (en) | 2018-09-27 | 2018-09-27 | A kind of asynchronous multitask sequence loading method based on Android |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN109388505B (en) |
Families Citing this family (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN110941459A (en) * | 2019-11-20 | 2020-03-31 | 东软集团股份有限公司 | Method and device for calling Replugin |
CN115904542B (en) * | 2022-12-15 | 2024-01-30 | 润芯微科技(江苏)有限公司 | Asynchronous initialization task method and system applied to android system |
Citations (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN106775699A (en) * | 2016-12-09 | 2017-05-31 | 武汉斗鱼网络科技有限公司 | The method that UI is updated in asynchronous thread |
-
2018
- 2018-09-27 CN CN201811130892.1A patent/CN109388505B/en active Active
Patent Citations (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN106775699A (en) * | 2016-12-09 | 2017-05-31 | 武汉斗鱼网络科技有限公司 | The method that UI is updated in asynchronous thread |
Also Published As
Publication number | Publication date |
---|---|
CN109388505A (en) | 2019-02-26 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US9128736B1 (en) | Common scheduling and synchronization primitives | |
EP0241946B1 (en) | Information processing system | |
US5274813A (en) | Operation system having a migration function which moves saved data associated with an interrupted process to a different save area | |
US8904386B2 (en) | Running a plurality of instances of an application | |
US4812981A (en) | Memory management system improving the efficiency of fork operations | |
US11861505B2 (en) | Method and apparatus of executing dynamic graph for neural network computation | |
JPH0535454B2 (en) | ||
CN109388505B (en) | A kind of asynchronous multitask sequence loading method based on Android | |
CN109308213B (en) | Multi-task breakpoint debugging method based on improved task scheduling mechanism | |
CN117075930B (en) | Computing framework management system | |
CN113010265A (en) | Pod scheduling method, scheduler, memory plug-in and system | |
US9804903B2 (en) | Data processing apparatus for pipeline execution acceleration and method thereof | |
EP1760580A1 (en) | Processing operation information transfer control system and method | |
CN113407343A (en) | Service processing method, device and equipment based on resource allocation | |
CN112948136A (en) | Method for implementing asynchronous log record of embedded operating system | |
WO2018022303A1 (en) | Capturing commands in a multi-engine graphics processing unit | |
CN116561091A (en) | Log storage method, device, equipment and readable storage medium | |
WO2022237419A1 (en) | Task execution method and apparatus, and storage medium | |
CN113238715B (en) | Intelligent file system, configuration method thereof, intelligent auxiliary computing equipment and medium | |
CN115576685A (en) | Container scheduling method and device and computer equipment | |
CN109002343B (en) | Method and device for realizing batch creation of virtual machines | |
CN111061460B (en) | Universal software development platform based on embedded system | |
EP1227401B1 (en) | Task management device, method and program therefor | |
CN111078301A (en) | Multi-core arithmetic device and operation method thereof | |
US20020156758A1 (en) | System and method for dynamic addition and removal of object interfaces |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |