Android中的线程池与任务队列:
在Android手机开发的时候,考虑的资源问题是必须的。当然我们要注重线程的消耗资源的过程,线程在new的时候最消耗内存开销,而在运行的时候远远小于new的时候的内存开销。故我们可以考虑应用线程池与任务队列去解决线程的消耗问题。
我们在最初加载的时候,可以new一个线程类,单独线程需要之行的操作。代码如下:
1. DownloadTask.java:
package com.threadExecutorService.download; public class DownloadTask implements Runnable { public String name; public DownloadTask(String name) { this.name = name; } @Override public void run() { try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(name + " executed OK!"); } public String getFileId(){ return name; } }
2. 线程管理类 DownloadTaskManager.java
package com.threadExecutorService.download; import java.util.HashSet; import java.util.LinkedList; import java.util.Set; /* * 其中downloadTasks表示的是线程队列,taskIdSet是任务队列,作用就是用来管理线程队列,此程序用的是去重操作。已经下载过的文件,不会再次下载。 */ public class DownloadTaskManager { //请求线程队列 private LinkedList<DownloadTask> downloadTasks; //任务不能重复 private Set<String> taskIdSet; private static DownloadTaskManager downloadTaskManager; public static synchronized DownloadTaskManager getInstance(){ if (null == downloadTaskManager) { downloadTaskManager = new DownloadTaskManager(); } return downloadTaskManager; } private DownloadTaskManager() { downloadTasks = new LinkedList<DownloadTask>(); taskIdSet = new HashSet<String>(); } public void addDownloadTask(DownloadTask downloadTask){ synchronized (downloadTasks) { if (!isTaskRepeat(downloadTask.getFileId())) { downloadTasks.addLast(downloadTask); } } } public boolean isTaskRepeat(String fileId) { synchronized (taskIdSet) { if (taskIdSet.contains(fileId)) { return true; } else { System.out.println("下载管理器增加下载任务:"+ fileId); taskIdSet.add(fileId); return false; } } } public DownloadTask getDownloadTask() { synchronized (downloadTasks) { if (downloadTasks.size() > 0) { System.out.println("下载管理器增加下载任务:"+"取出任务"); DownloadTask downloadTask = downloadTasks.removeFirst(); return downloadTask; } } return null; } }
3.线程池
DownloadTaskManagerThread.java
package com.threadExecutorService.download; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class DownloadTaskManagerThread implements Runnable{ private DownloadTaskManager downloadTaskManager ; // 创建一个可重用固定线程数的线程池 private ExecutorService pool; // 线程池大小 private final int POOL_SIZE = 5; // 轮询时间 private final int SLEEP_TIME = 1000; // 是否停止 private boolean isStop = false; public DownloadTaskManagerThread() { this.downloadTaskManager = DownloadTaskManager.getInstance(); pool = Executors.newFixedThreadPool(POOL_SIZE); } @Override public void run() { while (!isStop) { DownloadTask downloadTask = downloadTaskManager.getDownloadTask(); if (downloadTask != null) { pool.execute(downloadTask); } else {//如果当前任务队列中没有下载任务downloadTask try { // 查询任务完成失败的,重新加载任务队列 // 轮询 Thread.sleep(SLEEP_TIME); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } if (isStop) { pool.shutdown(); } } public void setStop(boolean isStop) { this.isStop = isStop; } }
4.测试类:Test.java
package com.threadExecutorService.test; import com.threadExecutorService.download.DownloadTask; import com.threadExecutorService.download.DownloadTaskManager; import com.threadExecutorService.download.DownloadTaskManagerThread; public class Test { public static void main(String[] args) { //1.new一个线程管理队列 DownloadTaskManager downloadTaskManager = DownloadTaskManager.getInstance(); //2.new一个线程池,并启动 DownloadTaskManagerThread downloadTaskManagerThread = new DownloadTaskManagerThread(); new Thread(downloadTaskManagerThread).start(); //3.请求下载 String []items=new String[]{"阿凡达1","阿凡达2","阿凡达3","阿凡达4","阿凡达5","阿凡达6","阿凡达7","阿凡达1","阿凡达2"}; for (int i = 0; i < items.length; i++) { downloadTaskManager.addDownloadTask(new DownloadTask(items[i])); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
结果如下:
相同名字并不会继续执行,OK!
本文代码:http://download.csdn.net/detail/jdsjlzx/8454395