一、线程

在Android开发中,你不可能都在主线程中开发,毕竟要联网,下载数据,保存数据等操作,当然这就离不开线程。

(当然你可以在Android4.0以前的手机里在主线程请求网络,我最早开发的时候,用的手机比较古老。。。)

在Android中你可以随意创建线程,于是就会造成线程不可控,内存泄漏,创建线程消耗资源,线程太多了消耗资源等问题。
具体线程怎么创建我就不在文章里描述了,毕竟这主要将并发编程。。。。

大家知道线程不可控就好了。。。于是就需要对线程进行控制,防止一系列问题出现,这就用到了如下要讲的东西。

二、线程池

线程池:顾名思义,就是放线程的大池子。

如何创建一个线程池?

先说说几个系统的线程池:

  1. FixedThreadPool 创建定长线程的线程池
  2. CachedThreadPool 需要的时候建立新的线程,超时线程销毁
  3. SingleThreadPool 单个线程的线程池
  4. ScheduledThreadPool 可以定时的线程池,创建周期性的任务

这几个线程池不做多余阐述,因为这些线程池的原理都与我下面要讲的有关。。。。

如何自定义线程池(先来了解几个必须知道的参数):

corePoolSize:

核心线程池大小,线程池中主要工作的线程的多少。

maximumPoolSize:

线程池最大线程数。

keepAliveTime:

空闲线程可保持的时间是多久,如果你启用了allowCoreThreadTimeOut方法,你的线程池里的空闲线程在这个时间段后会自动销毁,如果没启用,则只要不超过corePoolSize,空闲线程也不会销毁。

Unit:

keepAliveTime的时间单位

workQueue:

阻塞队列,当任务达到corePoolSize,就会被放入这个队列

常见几种BlockingQueue实现

  1. ArrayBlockingQueue :  有界的数组队列
  2. LinkedBlockingQueue : 可支持有界/无界的队列,使用链表实现
  3. PriorityBlockingQueue : 优先队列,可以针对任务排序
  4. SynchronousQueue : 队列长度为1的队列,和Array有点区别就是:client thread提交到block queue会是一个阻塞过程,直到有一个worker thread连接上来poll task。

threadFactory:

线程工厂,主要用来创建线程;

handler:

表示当拒绝处理任务时的策略,也就是参数maximumPoolSize达到后丢弃处理的方法。有以下四种取值:

  1. ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。
  2. ThreadPoolExecutor.DiscardPolicy:也是丢弃任务,但是不抛出异常。
  3. ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新尝试执行任务(重复此过程)
  4. ThreadPoolExecutor.CallerRunsPolicy:由调用线程处理该任务

用户也可以实现接口RejectedExecutionHandler定制自己的策略。

代码展示:

//线程工厂
public class TaskThreadFactory implements ThreadFactory {

 private final AtomicInteger mThreadNumber = new AtomicInteger(1);



 private final String mNamePrefix;

 TaskThreadFactory(String name) {
  mNamePrefix = name   "#";
 }

 public Thread newThread(Runnable r) {
  Thread t = new Thread(r,mNamePrefix   mThreadNumber.getAndIncrement());

//  if (t.isDaemon())
//   t.setDaemon(false);
//
//  if (t.getPriority() != Thread.NORM_PRIORITY)
//   t.setPriority(Thread.NORM_PRIORITY);

  return t;
 }
}

//重写runnable
public class PRunnable implements Runnable {

 public static final int HIGH = 1;//优先级高
 public static final int NORMAL = 2;//优先级中等
 public static final int LOW = 3;//优先级低
 @IntDef({HIGH,NORMAL,LOW})
 @Retention(RetentionPolicy.SOURCE)
 public @interface Priority{}

 public final int priority;
 private final Runnable runnable;
 public int serial;

 public PRunnable(Runnable runnable){
  this(NORMAL,runnable);
 }

 public PRunnable(@Priority int priority,Runnable runnable){
  this.priority = priority;
  this.runnable = runnable;
 }

 @Override
 public void run() {
  if (runnable != null) {
   runnable.run();
  }
 }

 /**
  * 线程队列方式 先进先出
  * @param r1
  * @param r2
  * @return
  */
 public static final int compareFIFO(PRunnable r1, PRunnable r2) {
  int result = r1.priority-r2.priority;
  return result==0?r1.serial-r2.serial:result;
 }

 /**
  * 线程队列方式 后进先出
  * @param r1
  * @param r2
  * @return
  */
 public static final int compareLIFO(PRunnable r1, PRunnable r2) {
  int result = r1.priority-r2.priority;
  return result==0?r2.serial-r1.serial:result;
 }
}

//线程池实现
public class TaskExecutor implements Executor {

 private final static int QUEUE_INIT_CAPACITY = 20;

 private static final int CORE = 3;

 private static final int MAX = 5;

 private static final int TIMEOUT = 30 * 1000;

 private AtomicInteger SERIAL = new AtomicInteger(0);//主要获取添加任务

 public static class Config {
  public int core;

  public int max;

  public int timeout;

  public boolean allowCoreTimeOut;

  public boolean fifo;

  public Config(int core, int max, int timeout, boolean allowCoreTimeOut,boolean fifo) {
   this.core = core;
   this.max = max;
   this.timeout = timeout;
   this.allowCoreTimeOut = allowCoreTimeOut;
   this.fifo = fifo;
  }
 }

 public static Config defaultConfig = new Config(CORE, MAX, TIMEOUT, true,true);


 private final String name;

 private final Config config;

 private ExecutorService service;

 public TaskExecutor(String name) {
  this(name, defaultConfig);
 }

 public TaskExecutor(String name, Config config) {
  this(name, config, true);
 }

 public TaskExecutor(String name, Config config, boolean startup) {
  this.name = name;
  this.config = config;

  if (startup) {
   startup();
  }
 }

 public void startup() {
  synchronized (this) {
   if (service != null && !service.isShutdown()) {
    return;
   }

   service = createExecutor(config);
  }
 }

 public void shutdown() {
  ExecutorService executor = null;

  synchronized (this) {
   // 交换变量
   if (service != null) {
    executor = service;
    service = null;
   }
  }

  if (executor != null) {
   // 停止线程
   if (!executor.isShutdown()) {
    executor.shutdown();
   }

   // 回收变量
   executor = null;
  }
 }

 private void executeRunnable(PRunnable runnable) {
  synchronized (this) {
   if (service == null || service.isShutdown()) {
    return;
   }
   runnable.serial = SERIAL.getAndIncrement();
   service.execute(runnable);
  }
 }

 @Override
 public void execute(Runnable runnable) {
  if (runnable instanceof PRunnable) {
   executeRunnable((PRunnable) runnable);
  }else{
   executeRunnable(new PRunnable(runnable));
  }
 }

 public Future<?> submit(Runnable runnable) {
  synchronized (this) {
   if (service == null || service.isShutdown()) {
    return null;
   }
   if (runnable instanceof PRunnable) {
    ((PRunnable) runnable).serial = SERIAL.getAndIncrement();
    return service.submit(runnable);
   }else{
    PRunnable pRunnable = new PRunnable(runnable);
    pRunnable.serial = SERIAL.getAndIncrement();
    return service.submit(pRunnable);
   }
  }
 }

 public void execute(Runnable runnable, @PRunnable.Priority int priority) {
  executeRunnable(new PRunnable(priority,runnable));
 }

 private ExecutorService createExecutor(Config config) {
  ThreadPoolExecutor service = new ThreadPoolExecutor(config.core, config.max, config.timeout,
    TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>(QUEUE_INIT_CAPACITY, config.fifo ? mQueueFIFOComparator : mQueueLIFOComparator),
    new TaskThreadFactory(name), new ThreadPoolExecutor.DiscardPolicy());

  allowCoreThreadTimeOut(service, config.allowCoreTimeOut);

  return service;
 }

 public boolean isBusy() {
  synchronized (this) {
   if (service == null || service.isShutdown()) {
    return false;
   }
   if(service instanceof ThreadPoolExecutor){
    ThreadPoolExecutor tService = (ThreadPoolExecutor) service;
    return tService.getActiveCount() >= tService.getCorePoolSize();
   }
   return false;
  }
 }

 private static final void allowCoreThreadTimeOut(ThreadPoolExecutor service, boolean value) {
  if (Build.VERSION.SDK_INT >= 9) {
   allowCoreThreadTimeOut9(service, value);
  }
 }

 @TargetApi(9)
 private static final void allowCoreThreadTimeOut9(ThreadPoolExecutor service, boolean value) {
  service.allowCoreThreadTimeOut(value);
 }


 Comparator<Runnable> mQueueFIFOComparator = new Comparator<Runnable>() {

  @Override
  public int compare(Runnable lhs, Runnable rhs) {
   PRunnable r1 = (PRunnable) lhs;
   PRunnable r2 = (PRunnable) rhs;

   return PRunnable.compareFIFO(r1, r2);
  }
 };

 Comparator<Runnable> mQueueLIFOComparator = new Comparator<Runnable>() {

  @Override
  public int compare(Runnable lhs, Runnable rhs) {
   PRunnable r1 = (PRunnable) lhs;
   PRunnable r2 = (PRunnable) rhs;

   return PRunnable.compareLIFO(r1, r2);
  }
 };

}

以上所述是小编给大家介绍的Android开发经验谈:并发编程(线程与线程池)详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对Devmax网站的支持!

Android开发经验谈:并发编程(线程与线程池)(推荐)的更多相关文章

  1. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Html5 video标签视频的最佳实践

    这篇文章主要介绍了Html5 video标签视频的最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  3. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  4. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  5. iOS:核心图像和多线程应用程序

    我试图以最有效的方式运行一些核心图像过滤器.试图避免内存警告和崩溃,这是我在渲染大图像时得到的.我正在看Apple的核心图像编程指南.关于多线程,它说:“每个线程必须创建自己的CIFilter对象.否则,你的应用程序可能会出现意外行为.”这是什么意思?我实际上是试图在后台线程上运行我的过滤器,所以我可以在主线程上运行HUD(见下文).这在coreImage的上下文中是否有意义?

  6. ios – 多个NSPersistentStoreCoordinator实例可以连接到同一个底层SQLite持久性存储吗?

    我读过的关于在多个线程上使用CoreData的所有内容都讨论了使用共享单个NSPersistentStoreCoordinator的多个NSManagedobjectContext实例.这是理解的,我已经使它在一个应用程序中工作,该应用程序在主线程上使用CoreData来支持UI,并且具有可能需要一段时间才能运行的后台获取操作.问题是NSPersistentStoreCoordinator会对基础

  7. ios – XCode断点应该只挂起当前线程

    我需要调试多线程错误.因此,为了获得生成崩溃的条件,我需要在代码中的特定点停止一个线程,并等待另一个线程到达第二个断点.我现在遇到的问题是,如果一个线程遇到断点,则所有其他线程都被挂起.有没有办法只停止一个线程,让其他线程运行,直到它们到达第二个断点?)其他更有趣的选择:当你点击第一个断点时,你可以进入控制台并写入这应该在该断点处暂停当前上下文中的线程一小时.然后在Xcode中恢复执行.

  8. ios – 在后台线程中写入Realm后,主线程看不到更新的数据

    >清除数据库.>进行API调用以获取新数据.>将从API检索到的数据写入后台线程中的数据库中.>从主线程上的数据库中读取数据并渲染UI.在步骤4中,数据应该是最新数据,但我们没有看到任何数据.解决方法具有runloops的线程上的Realm实例,例如主线程,updatetothelatestversionofthedataintheRealmfile,因为通知被发布到其线程的runloop.在后台

  9. Ionic – Splash Screen适用于iOS,但不适用于Android

    我有一个离子应用程序,其中使用CLI命令离子资源生成的启动画面和图标iOS版本与正在渲染的启动画面完美配合,但在Android版本中,只有在加载应用程序时才会显示白屏.我检查了config.xml文件,所有路径看起来都是正确的,生成的图像出现在相应的文件夹中.(我使用了splash.psd模板来生成它们.我错过了什么?这是config.xml文件供参考,我觉得我在这里做错了–解决方法在config.xml中添加以下键:它对我有用!

  10. ios – 无法启动iPhone模拟器

    /Library/Developer/CoreSimulator/Devices/530A44CB-5978-4926-9E91-E9DBD5BFB105/data/Containers/Bundle/Application/07612A5C-659D-4C04-ACD3-D211D2830E17/ProductName.app/ProductName然后,如果您在Xcode构建设置中选择标准体系结构并再次构建和运行,则会产生以下结果:dyld:lazysymbolbindingFailed:Symbol

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部