Thread Pool and task queue in Android

Source: Internet
Author: User

Thread Pool and task queue in Android:

When developing Android phones, you must consider resource issues. Of course, we should pay attention to the process of thread resource consumption. The thread consumes the most memory overhead when it is new, and the memory overhead when it is running is far smaller than that when it is new. Therefore, we can consider the application thread pool and task queue to solve the thread consumption problem.

Example:
We can create a new Thread class during initial loading and perform operations that individual threads need. The Code is as follows:

1. downloadtask. Java:

package com.threadExecutorService.test;public class DownloadTask implements Runnable{public String name;public DownloadTask(String name){this.name=name;}@Overridepublic void run() {//String name=Thread.currentThread().getName();try {Thread.sleep(1500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(name + " executed OK!");}public String getFileId(){return name;}}

2. thread management class downloadtaskmanager. Java

Package COM. threadexecutorservice. test; import Java. util. hashset; import Java. util. using list; import Java. util. set; public class downloadtaskmanager {Private Static final string tag = "downloadtaskmanager"; // UI Request queue private volume list <downloadtask> downloadtasks; // The private set <string> taskidset cannot be repeated; private Static downloadtaskmanager downloadtaskmananger; private downloadtaskmanager () {downloadtasks = ne W linkedlist <downloadtask> (); taskidset = new hashset <string> ();} public static synchronized downloadtaskmanager getinstance () {If (downloadtaskmananger = NULL) {downloadtaskmananger = new downloadtaskmanager ();} return downloadtaskmananger;} // 1. run public void adddownloadtask (downloadtask) {synchronized (downloadtasks) {If (! Istaskrepeat (downloadtask. getfileid () {// added the downloadtasks download task. addlast (downloadtask) ;}} public Boolean istaskrepeat (string fileid) {synchronized (taskidset) {If (taskidset. contains (fileid) {return true;} else {system. out. println ("Download Manager added download task:" + fileid); taskidset. add (fileid); Return false ;}} public downloadtask getdownloadtask () {synchronized (downloadtasks) {If (downloadtasks. size ()> 0) {system. out. println ("Download Manager added download task:" + "retrieve task"); downloadtask = downloadtasks. removefirst (); Return downloadtask;} return NULL ;}}

Downloadtasks indicates the thread queue, taskidset indicates the task queue, and is used to manage the thread queue. This program uses deduplication. Files that have been downloaded will not be downloaded again.

3. Thread Pool

Downloadtaskmanagerthread. Java

/*** @ Title: requesttaskthread. java * @ package COM. kingsoft. filesystem * @ Description: Todo (using a sentence to describe what the file is doing) * @ author zengzhaoshuai zengzhaoshuai@kingsoft.com * @ date 2012-2-25 am 10:35:00 * @ version V1.0 */package COM. threadexecutorservice. test; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; public class downloadtaskmanagerthread implements runnable {private downloa Dtaskmanager downloadtaskmanager; // create a thread pool private executorservice pool with a fixed number of reusable threads; // The thread pool size private final int pool_size = 5; // polling time private final int sleep_time = 1000; // whether to stop private Boolean isstop = false; Public downloadtaskmanagerthread () {downloadtaskmanager = downloadtaskmanager. getinstance (); pool = executors. newfixedthreadpool (pool_size) ;}@ overridepublic void run () {// todo auto-generated Method Stubwhile (! Isstop) {downloadtask = downloadtaskmanager. getdownloadtask (); If (downloadtask! = NULL) Restart pool.exe cute (downloadtask);} else {// if no downloadtask has been completed in the task queue try {// If the Query Task fails, reload the task queue // Round-Robin, thread. sleep (sleep_time);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}if (isstop) {pool. shutdown () ;}/ *** @ Param isstop * The isstop to set */Public void setstop (Boolean isstop) {This. isstop = isstop ;}}

3. Test class: Test. Java

Package COM. threadexecutorservice. test; public class test {public static void main (string [] ARGs) {// 1. A new thread manages the queue downloadtaskmanager. getinstance (); // 2. A new thread pool starts downloadtaskmanagerthread = new downloadtaskmanagerthread (); New thread (downloadtaskmanagerthread ). start (); // 3. request to download string [] items = new string [] {" 1", "2"," 3", "4 ", "To Chen Yu 5", "to Chen Yu 6", "to Chen Yu 7", "to Chen Yu 1", "to Chen Yu 2"}; For (INT I = 0; I <items. length; I ++) {downloadtaskmanager downloadtaskmananger = downloadtaskmanager. getinstance (); downloadtaskmananger. adddownloadtask (New downloadtask (items [I]); try {thread. sleep (2000);} catch (interruptedexception e) {e. printstacktrace ();}}}}

The result is as follows:

The execution will not continue with the same name. OK !~

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.