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);
    }

    }

  • 相关阅读:
    web10 动态action的应用
    web09 struts2配置 struts2入门
    web 08 struts2入门 struts2配置 struts包
    web07-jdbcBookStore
    web06-PanduanLogin
    web05-CounterServlet
    web04-LoginServlet
    web03-OutputInfo
    web02-welcomeyou
    web01-helloworld
  • 原文地址:https://www.cnblogs.com/ljbky/p/4828763.html
Copyright © 2011-2022 走看看