Builder mode, optimizing the real case! !

We take cases on a multi-threaded download, the model builders to use to optimize it.
Directly on the code, the code is best explained:

The final call to effects such as:

        // 构建者模式初始化数据。
        DownloadConfig builder = new DownloadConfig.Builder()
                .setCoreThreadSize(2)
                .setMaxThreadSize(2)
                .setLoadProgressThreadSize(1)
                .builder();

        DownloadManager.getInstance().init(builder);

Take a look at how to achieve:

/**
 * <p>Title: DownloadConfig</p >
 * <p>Description: TODO</p >
 * <p>Company: ihaveu</p >
 *
 * @author MaWei
 * @date 2018/3/8
 */
public class DownloadConfig {

    /** 核心线程数量*/
    private int coreThreadSize;
    /** 最大线程数量*/
    private int maxThreadSize;
    /** 检测进度线程的数量*/
    private int loadProgressThreadSize;

    public int getCoreThreadSize() {
        return coreThreadSize;
    }

    public int getMaxThreadSize() {
        return maxThreadSize;
    }

    public int getLoadProgressThreadSize() {
        return loadProgressThreadSize;
    }

    public DownloadConfig(Builder builder) {
        // TODO: 如果没有调用set方法设值,那么默认值是Download默认的值  
        this.coreThreadSize = coreThreadSize == 0 ? DownloadManager.CORE_THREAD : builder.coreThreadSize;
        this.maxThreadSize = maxThreadSize == 0 ? DownloadManager.MAX_THREAD : builder.maxThreadSize;
        this.loadProgressThreadSize = loadProgressThreadSize == 0 ? DownloadManager.LOCAL_PROGRESS_SIZE : builder.loadProgressThreadSize;
    }

    public static class Builder{
        /** 核心线程数量*/
        private int coreThreadSize;
        /** 最大线程数量*/
        private int maxThreadSize;
        /** 检测进度线程的数量*/
        private int loadProgressThreadSize;

        public Builder setCoreThreadSize(int coreThreadSize) {
            this.coreThreadSize = coreThreadSize;
            return this;
        }

        public Builder setMaxThreadSize(int maxThreadSize) {
            this.maxThreadSize = maxThreadSize;
            return this;
        }

        public Builder setLoadProgressThreadSize(int loadProgressThreadSize) {
            this.loadProgressThreadSize = loadProgressThreadSize;
            return this;
        }

        public DownloadConfig builder(){
            return new DownloadConfig(this);
        }
    }
}

The final look at how to use the download:

public class DownloadManager {

    public static DownloadManager mManager = new DownloadManager();
    /** 最大线程数量*/
    public final static int MAX_THREAD = 2;
    /** 核心线程数量*/
    public final static int CORE_THREAD = 2;
    /** 线程的数量*/
    public final static int LOCAL_PROGRESS_SIZE = 1;
    /** 读取数据库数据*/
    public List<DownloadEntity> mCache;
    /** 创建一个线程,用来一直检测下载文件的长度,从而得到下载进度*/
    public static ExecutorService sLocalProgressPool;
    /** 创建下载线程池*/
    public ThreadPoolExecutor sThreadPool;

    /**
     * 初始化操作
     */
    public void init(DownloadConfig config){
        /**
         * 创建线程池
         * 参数1:核心线程数量
         * 参数2:线程池最大数量
         * 参数3:线程存活时间
         * 参数4:设置时间等级
         * 参数5:先不管
         * 原理:线程池首先会创建核心线程, 如果在执行时,超过最大数量会抛出异常
         */
        sThreadPool = new ThreadPoolExecutor(config.getCoreThreadSize(), config.getMaxThreadSize(), 60,
                TimeUnit.MICROSECONDS, new LinkedBlockingDeque<Runnable>(), new ThreadFactory() {

            private AtomicInteger mInteger = new AtomicInteger(1);
            @Override
            public Thread newThread(Runnable runnable) {
                // 指定Runnable  和线程名称
                Thread mThread = new Thread(runnable, "download thread #" + mInteger.getAndIncrement());
                return mThread;
            }
        });

        /** 创建一个线程,用来一直检测下载文件的长度,从而得到下载进度*/
        sLocalProgressPool = Executors.newFixedThreadPool(config.getLoadProgressThreadSize());
    }

}

OK, model builders, pretty good, scalability, coupling, modifications are every minute is! Hope you can learn!

Published 51 original articles · won praise 78 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_39079048/article/details/79486509
Recommended