Android开发之线程池详解(2)自定义线程池

这里我模仿newFixedThreadPool写了一个定长线程池。
废话少说,先上代码:

package com.example.nine;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class MyThreadPool {
    private List<Runnable> taskQue;//任务队列
    private List<MyThread> threads;//线程池
    private int size;//线程池缓存的最大线程数
    private final long maxTime = 1000 * 600;//线程空闲的最长时间,超过此时间,则销毁
    public static MyThreadPool myThreadPool;

    public MyThreadPool(int size) {
        if (size <= 0) {
            size = 5;
        }
        this.size = size;
        init();
    }

    public static MyThreadPool creatMyThreadPool(int size) {
        if (myThreadPool == null) {
            myThreadPool = new MyThreadPool(size);
        }
        return myThreadPool;
    }

    private void init() {
        taskQue = Collections.synchronizedList(new LinkedList<Runnable>());//这里为了保证线程安全
        threads = Collections.synchronizedList(new LinkedList<MyThread>());
        for (int i = 0; i < size; i++) {
            threads.add(new MyThread());
        }
    }

    private void start() {

        for (int i = 0; i < taskQue.size(); i++) {
            //先判断是否有空闲的,如果有直接用
            if (threads.get(i).getThread() != null && !threads.get(i).isRunning) {
                break;
            }
            //如果没有空闲线程,并且线程池内的缓存线程数小于最大数,则新建线程,否则等待
            if (i < size && threads.get(i).getThread() == null) {
                final int position = i;
                Thread thread = new Thread() {
                    @Override
                    public void run() {

                        while (threads.get(position).getTime() < maxTime) {

                            if (threads.get(position).getTask() == null) {
                                if (taskQue.size() > 0) {
                                    threads.get(position).setTask(taskQue.get(0));
                                    taskQue.remove(0);
                                }
                            }
                            if (threads.get(position).getTask() != null) {
                                threads.get(position).setRunning(true);
                                threads.get(position).getTask().run();
                                threads.get(position).setTask(null);
                                threads.get(position).setTime(0);

                            }
                            try {
                                threads.get(position).setRunning(false);
                                Thread.sleep(500);
                                threads.get(position).setTime(threads.get(position).getTime() + 500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        threads.get(position).setThread(null);
                    }
                };
                threads.get(i).setThread(thread);
                threads.get(position).getThread().start();
                break;
            }
        }

    }

    public void addTask(Runnable runnable) {
        taskQue.add(runnable);
        start();
    }

    class MyThread {
        private Thread thread;
        private long time;
        private Runnable task;
        private boolean isRunning;
        public boolean isRunning() {
            return isRunning;
        }

        public void setRunning(boolean isRunning) {
            this.isRunning = isRunning;
        }

        public Runnable getTask() {
            return task;
        }

        public void setTask(Runnable task) {
            this.task = task;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public MyThread() {
            super();
        }

        public Thread getThread() {
            return thread;
        }

        public void setThread(Thread thread) {
            this.thread = thread;
        }

        public MyThread(Thread thread) {
            super();
            this.thread = thread;
        }

    }
}

先说下,我这个线程池的功能。

**1。线程池可以自定义最大线程数,默认是5.
2。当有新任务加入时,如果有空闲的线程,则复用,如果没有并且线程池内数量小于最大数,则新建。
3.。这里我自己设置了一个最大等待时间,也就是说,如果单个线程空闲时间超过了这个时间,那么则停止等待,线程置空。这可以避免长期存在空闲线程,占用系统资源。**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值