Android asynchronous task code is very short, simple asynchronous tool encapsulation, easy to execute asynchronous tasks

Source: Internet
Author: User

Android asynchronous task code is very short, simple asynchronous tool encapsulation, easy to execute asynchronous tasks

It is relatively simple. There are three classes.

1. Create a single-instance thread pool to execute asynchronous tasks

package com.ferris.pool;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class FerrisPoolManager {private ExecutorService service;private FerrisPoolManager() {int num = Runtime.getRuntime().availableProcessors();service = Executors.newFixedThreadPool(num * 2);}private static FerrisPoolManager manager;public static FerrisPoolManager getInstance() {if (manager == null) {synchronized (FerrisPoolManager.class) {if(manager == null) {manager = new FerrisPoolManager();}}}return manager;}public void addTask(Runnable runnable) {service.execute(runnable);}public void cancelTask(){service.shutdown();}}



2. Define a task interface

Package com. ferris. pool; public interface FerrisTaskListem {public void get (); // run public void update () in the thread; // run in the main thread (where is the new thread, not necessarily the main thread service )}



3. task execution object

package com.ferris.pool;import android.os.Handler;import android.os.Message;public class FerrisTask {private FerrisTaskListem taskListem;private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {if(taskListem!=null){taskListem.update();}};};public FerrisTask(FerrisTaskListem taskListem) {this.taskListem = taskListem;}public void Execute(){FerrisPoolManager.getInstance().addTask(new RunTask());}public class RunTask implements Runnable {@Overridepublic void run() {// TODO Auto-generated method stubif(taskListem!=null){taskListem.get();}Message message=Message.obtain();handler.sendMessage(message);}}}


4. Usage


FerrisTask ferrisTask = new FerrisTask (new FerrisTaskListem () {@ Overridepublic void update () {// main Thread // TODO Auto-generated method stubString name = Thread. currentThread (). getName (); System. out. print (name) ;}@ Overridepublic void get () {// UI Thread // TODO Auto-generated method stubString name = Thread. currentThread (). getName (); System. out. print (name) ;}}); ferrisTask. execute ();




Related Article

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.