Ps:force is meaningless without Skill
Learning content:
1. Use Andbase to implement single-threaded tasks ...
2. Use Andbase for multi-threaded tasks ...
Andbase internally encapsulates a variety of methods that we can use to build single-threaded and multi-threaded tasks. Generic thread tasks are used to perform time-consuming operations ... For example, download what installs the package, the file and so on the data quantity big thing, we must have to open an asynchronous thread or is the synchronous thread to perform the operation ... If there are too many tasks, then we can use a task request queue or a thread pool to handle multiple tasks ... This can save a lot of time ...
The class involved is the majority of the classes in the Com.ab.task package: Abthread.java, Abtaskpool.java, Abtaskqueue.java, Abtask.java is the implementation of the task scheduling class. Features are not the same. But the overall processing task of the addition and processing mode is basically the same ...
You first need to set up the thread object to process the task ... Both the asynchronous thread, the synchronization thread, or the thread pool need to establish the object first: Then add the task ... After you add it, the thread processes the task. Finally return the result: The creation of the task requires the use of Abtaskitem...item to bind the previous position,listener,result. These three variables represent the task number of the current task, the task execution listener, and the result after execution:
The listener of the task needs to be bound. Bind by Abtasklistener: This listener consists of two methods: One is the method in the execution process: That's what the task needs to do. Get () function ... One is the action that needs to be performed after the task is completed ... update () function:
1. Use Andbase to implement single-threaded tasks ...
Implementing single-threaded tasks is very simple. The implementation is made up of two ways: One is to use the handler message mechanism to handle the current task, one is to use Asynctask to handle the current task ...
Let's start with the handler message thread executing the task ...
Public voidThreadclick (View v) {//a single thread executes a task ...//Create a Thread object :Abthread thread=NewAbthread (); //Define a Task object ... FinalAbtaskitem item=NewAbtaskitem (); Item.listener=NewAbtasklistener () {@Override Public voidUpdate () {Showtoast ("Execution Complete"); } @Override Public voidget () {//time-consuming operation ...Showtoastinthread ("Start Execution");//This step must use a thread to display the toast box: Because the current thread is already working on the current task : Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }; }; Thread.execute (item); //join the task to the processing thread ...}
This process is basically the same as what I said above ... First set up a task execution object ... Then define a task request: Set the related listener for this request. Implement the internal method ... When the task processing is complete: Call the handler message mechanism to finish sending the results ... Finally, the method in update () is used to tell the task has been processed ...
Then take a look at the asynchronous thread to perform a single task ...
The asynchronous thread performs the task actually the same ... Asynchronous thread inheritance with Asynctask: Basic should be clear asynctask, background thread: You can update the UI interface by performing tasks in the background ...
Public voidTaskclick (View v) {//Asynchronous Task Execution :Abtask task=NewAbtask (); //define Task Object ...Abtaskitem Item=NewAbtaskitem (); Item.listener=NewAbtasklistener () {@Override Public voidget () {Showtoastinthread ("Start Execution"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} @Override Public voidUpdate () {Showtoast ("Execution Complete"); } }; Task.execute (item); }
The pattern is basically the same, but one is to use the inherited Asynctask to implement asynchronous loading. One uses the handler Message ...
2. Use Andbase for multi-threaded tasks ...
Multi-threaded tasks are actually very simple ... One is to use the task queue to save the task first, the task queue to the thread pool, the line pool for related processing ... The other is to add the task directly inside the thread pool. Task related processing by thread pool ...
Use the task queue to add the task to the queue, and then submit the current queue to the thread pool. The processing of these tasks is done by the thread pool:
Public voidQueueclick (View v) {//thread Task Queue ...Abtaskqueue queue=NewAbtaskqueue (); //Define a task :Abtaskitem item=NewAbtaskitem (); Abtaskitem item_1=NewAbtaskitem (); Item.listener=NewAbtasklistener () {@Override Public voidUpdate () {Showtoast ("Execution Complete"); } @Override Public voidget () {Showtoastinthread ("Task Execution 1"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }; Item_1.listener=NewAbtasklistener () {@Override Public voidUpdate () {Showtoast ("Execution Complete"); } @Override Public voidget () {Showtoastinthread ("Task Execution 2"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }; Queue.execute (item); Queue.execute (item_1); //Force stop://Queue.quit (); //force the previous request to stop://Queue.execute (Item_1, true); }
Take the task directly to the thread pool, and then the task is processed by the line pool ... The pattern difference between the two is not very big ...
Public voidPoolclick (View v) {//task thread pool ...Abtaskpool pool=abtaskpool.getinstance (); Abtaskitem Item=NewAbtaskitem (); Abtaskitem item_1=NewAbtaskitem (); Item.listener=NewAbtasklistener () {@Override Public voidget () {Showtoastinthread ("Start execution 1"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} @Override Public voidUpdate () {removeprogressdialog (); Showtoast ("Execution Complete 1"); } }; Item_1.listener=NewAbtasklistener () {@Override Public voidget () {Showtoastinthread ("Start Execution 2"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} @Override Public voidUpdate () {removeprogressdialog (); Showtoast ("Task Complete 2"); } }; Pool.execute (item); Pool.execute (item_1); }
Android Learning Note andbase Framework Learning (iv) using encapsulated functions for single, multithreaded tasks