zoukankan      html  css  js  c++  java
  • Android单例线程池

    package com.jredu.schooltong.manager;

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    public class ExecutorManager {
    // 1.私有构造函数,提供静态变量用以存储,提供静态的方法
    private ExecutorManager() {
    init();

    }

    // 静态变量
    private static ExecutorManager instance;

    // 静态方法
    public static ExecutorManager getInstance() {

    if (instance == null) {
    //加锁
    synchronized (ExecutorManager.class) {
    if (instance == null) {
    instance = new ExecutorManager();
    }
    }
    }
    return instance;
    }

    // 运用单例模式,保证只能够被实例化一次
    // 这个类就能应用到其他APP中了
    private ExecutorService executorService;

    // 初始化线程池
    private void init() {
    int threadCount = Runtime.getRuntime().availableProcessors() * 2 + 1;
    executorService = Executors
    .newFixedThreadPool(Math.min(threadCount, 8));

    }
    public ExecutorService getExecutor(){
    return this.executorService;
    }
    public void execute(Runnable runnable){
    this.executorService.execute(runnable);
    }

    }

  • 相关阅读:
    【nodejs】使用Node.js实现REST Client调用REST API
    [Groovy] Groovy && JNI
    一些很实用的Maven Plugins
    秋天到了
    今天头好疼
    想起那个人的时候
    不知道标题
    生活让我懂得
    显示实现接口和实现接口
    Foreach原理
  • 原文地址:https://www.cnblogs.com/ljbky/p/4828763.html
Copyright © 2011-2022 走看看