本文是我看过网上的一些自定义的线程池自己修改后用到Android上的一个例子,
主要是学习线程池的实现原理。下面是我看的原文地址
http://blog.csdn.net/hsuxu/article/details/8985931
下文就是我写的一个简单的线程池:
package com.example.administrator.executorthread;
import java.util.LinkedList;
/**
* Created by Administrator on 2015/10/21.
* 简单的自定义线程池,用于理解线程池的概念
*/
public class MyExectorPool {
private static final int DEFAULT_NUM = 5;//默认线程的个数
private WorkThread[] workThreads;//线程池中的线程集合
private final LinkedList<Runnable> queue = new LinkedList<>();//任务队列
private static MyExectorPool exectorPool;
private MyExectorPool() {
this(DEFAULT_NUM);
}
private MyExectorPool(int num) {
workThreads = new WorkThread[num];
for (int a = 0; a < num; a++) {
workThreads[a] = new WorkThread();
workThreads[a].start();
}
}
/**
* 获取包含单个线程的线程池
*
* @return MyExectorPool 线程池
*/
public static MyExectorPool getInstance() {
if (exectorPool == null) {
synchronized (MyExectorPool.class) {
if (exectorPool == null) {
exectorPool = new MyExectorPool();
}
}
}
return exectorPool;
}
/**
* 单例模式
* 获取有num个线程的线程池
*
* @param num 线程的个数
* @return MyExectorPool 线程池
*/
public static MyExectorPool getInstance(int num) {
if (exectorPool == null) {
synchronized (MyExectorPool.class) {
if (exectorPool == null) {
if (num <= 0) {
num = DEFAULT_NUM;
}
exectorPool = new MyExectorPool(num);
}
}
}
return exectorPool;
}
/**
* 运行
*
* @param task 任务
*/
public void execute(Runnable task) {
synchronized (queue) {
queue.add(task);
queue.notify();
}
}
/**
* 销毁线程池
*/
public void destory() {
for (WorkThread work : workThreads) {
work.iSRunning = false;
}
workThreads = null;
}
/**
* 内部线程类,用于执行任务Runnable
*/
private class WorkThread extends Thread {
public boolean iSRunning = true;
@Override
public void run() {
Runnable r = null;
while (iSRunning) {
synchronized (queue) {//要保证任务队列的线程安全
if (!queue.isEmpty()) {
r = queue.remove(0);//队列先进先出
}
}
if (r != null) {
r.run();
}
r = null;
}
}
}
}
下面是测试代码
package com.example.administrator.executorthread;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
public static final String TAG = "MainActivity";
private MyExectorPool pool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pool = MyExectorPool.getInstance(5);
pool.execute(new MyRunnbale(1));
pool.execute(new MyRunnbale(2));
pool.execute(new MyRunnbale(3));
pool.execute(new MyRunnbale(4));
pool.execute(new MyRunnbale(5));
pool.execute(new MyRunnbale(6));
pool.execute(new MyRunnbale(7));
pool.execute(new MyRunnbale(8));
pool.execute(new MyRunnbale(9));
}
private class MyRunnbale implements Runnable {
private int id;
public MyRunnbale(int id) {
super();
this.id = id;
}
@Override
public void run() {
Log.e(TAG, "任务" + id + "开始");
for (int a = 0; a < 10; a++) {
try {
Thread.sleep(20 * (10 - id));
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG, " 线程+" + id + "正在运行");
try {
Thread.sleep(20 * (10 - id));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.e(TAG, "任务" + id + "完成");
}
}
@Override
protected void onDestroy() {
pool.destory();
super.onDestroy();
}
}