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

    Handler+Runnable模式

    我们先看一个并不是异步线程加载的例子,使用 Handler+Runnable模式。

    这里为何不是新开线程的原因请参看这篇文章:Android Runnable 运行在那个线程 这里的代码其实是在UI 主线程中下载图片的,而不是新开线程。

    我们运行下面代码时,会发现他其实是阻塞了整个界面的显示,需要所有图片都加载完成后,才能显示界面。

     1 package ghj1976.AndroidTest;
     2  
     3 import java.io.IOException;
     4 import java.net.URL;
     5 import android.app.Activity;
     6 import android.graphics.drawable.Drawable;
     7 import android.os.Bundle;
     8 import android.os.Handler;
     9 import android.os.SystemClock;
    10 import android.util.Log;
    11 import android.widget.ImageView;
    12  
    13 public class MainActivity extends Activity {
    14         @Override
    15         public void onCreate(Bundle savedInstanceState) {
    16                 super.onCreate(savedInstanceState);
    17                 setContentView(R.layout.main);
    18                 loadImage("http://www.baidu.com/img/baidu_logo.gif", R.id.imageView1);
    19                 loadImage(<img id="\"aimg_W4qPt\"" onclick="\"zoom(this," this.src,="" 0,="" 0)\"="" class="\"zoom\"" file="\"http://www.chinatelecom.com.cn/images/logo_new.gif\"" onmouseover="\"img_onmouseoverfunc(this)\"" onload="\"thumbImg(this)\"" border="\"0\"" alt="\"\"" src="\"http://www.chinatelecom.com.cn/images/logo_new.gif\"" lazyloaded="true">",
    20                                 R.id.imageView2);
    21                 loadImage("http://cache.soso.com/30d/img/web/logo.gif, R.id.imageView3);
    22                 loadImage("http://csdnimg.cn/www/images/csdnindex_logo.gif",
    23                                 R.id.imageView4);
    24                 loadImage("http://images.cnblogs.com/logo_small.gif",
    25                                 R.id.imageView5);
    26         }
    27  
    28         private Handler handler = new Handler();
    29  
    30         private void loadImage(final String url, final int id) {
    31                 handler.post(new Runnable() {
    32                         public void run() {
    33                                 Drawable drawable = null;
    34                                 try {
    35                                         drawable = Drawable.createFromStream(
    36                                                         new URL(url).openStream(), "image.gif");
    37                                 } catch (IOException e) {
    38                                         Log.d("test", e.getMessage());
    39                                 }
    40                                 if (drawable == null) {
    41                                         Log.d("test", "null drawable");
    42                                 } else {
    43                                         Log.d("test", "not null drawable");
    44                                 }
    45                                 // 为了测试缓存而模拟的网络延时 
    46                                 SystemClock.sleep(2000); 
    47                                 ((ImageView) MainActivity.this.findViewById(id))
    48                                                 .setImageDrawable(drawable);
    49                         }
    50                 });
    51         }
    52 }

    Handler+Thread+Message模式

    这种模式使用了线程,所以可以看到异步加载的效果。

     1 package ghj1976.AndroidTest;
     2  
     3 import java.io.IOException;
     4 import java.net.URL;
     5 import android.app.Activity;
     6 import android.graphics.drawable.Drawable;
     7 import android.os.Bundle;
     8 import android.os.Handler;
     9 import android.os.Message;
    10 import android.os.SystemClock;
    11 import android.util.Log;
    12 import android.widget.ImageView;
    13  
    14 public class MainActivity extends Activity {
    15         @Override
    16         public void onCreate(Bundle savedInstanceState) {
    17                 super.onCreate(savedInstanceState);
    18                 setContentView(R.layout.main);
    19                 loadImage2("http://www.baidu.com/img/baidu_logo.gif", R.id.imageView1);
    20                 loadImage2("http://www.chinatelecom.com.cn/images/logo_new.gif",
    21                                 R.id.imageView2);
    22                 loadImage2("http://cache.soso.com/30d/img/web/logo.gif", R.id.imageView3);
    23                 loadImage2("http://csdnimg.cn/www/images/csdnindex_logo.gif",
    24                                 R.id.imageView4);
    25                 loadImage2("http://images.cnblogs.com/logo_small.gif",
    26                                 R.id.imageView5);
    27         }
    28  
    29         final Handler handler2 = new Handler() {
    30                 @Override
    31                 public void handleMessage(Message msg) {
    32                         ((ImageView) MainActivity.this.findViewById(msg.arg1))
    33                                         .setImageDrawable((Drawable) msg.obj);
    34                 }
    35         };
    36  
    37         // 采用handler+Thread模式实现多线程异步加载
    38         private void loadImage2(final String url, final int id) {
    39                 Thread thread = new Thread() {
    40                         @Override
    41                         public void run() {
    42                                 Drawable drawable = null;
    43                                 try {
    44                                         drawable = Drawable.createFromStream(
    45                                                         new URL(url).openStream(), "image.png");
    46                                 } catch (IOException e) {
    47                                         Log.d("test", e.getMessage());
    48                                 }
    49  
    50                                 // 模拟网络延时
    51                                 SystemClock.sleep(2000);
    52  
    53                                 Message message = handler2.obtainMessage();
    54                                 message.arg1 = id;
    55                                 message.obj = drawable;
    56                                 handler2.sendMessage(message);
    57                         }
    58                 };
    59                 thread.start();
    60                 thread = null;
    61         }
    62  
    63 }

    Handler+ExecutorService(线程池)+MessageQueue模式

    能开线程的个数毕竟是有限的,我们总不能开很多线程,对于手机更是如此。

    这个例子是使用线程池。Android拥有与Java相同的ExecutorService实现,我们就来用它。

    线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理。当有线程任务时,从池中取一个,执行完成后线程对象归池,这样可以避免反复创建线程对象所带来的性能开销,节省了系统的资源。

     1 package ghj1976.AndroidTest;
     2  
     3 import java.io.IOException;
     4 import java.net.URL;
     5 import java.util.concurrent.ExecutorService;
     6 import java.util.concurrent.Executors;
     7  
     8 import android.app.Activity;
     9 import android.graphics.drawable.Drawable;
    10 import android.os.Bundle;
    11 import android.os.Handler;
    12 import android.os.Message;
    13 import android.os.SystemClock;
    14 import android.util.Log;
    15 import android.widget.ImageView;
    16  
    17 public class MainActivity extends Activity {
    18         @Override
    19         public void onCreate(Bundle savedInstanceState) {
    20                 super.onCreate(savedInstanceState);
    21                 setContentView(R.layout.main);
    22                 loadImage3("http://www.baidu.com/img/baidu_logo.gif", R.id.imageView1);
    23                 loadImage3("http://www.chinatelecom.com.cn/images/logo_new.gif",
    24                                 R.id.imageView2);
    25                 loadImage3("http://cache.soso.com/30d/img/web/logo.gif",
    26                                 R.id.imageView3);
    27                 loadImage3("http://csdnimg.cn/www/images/csdnindex_logo.gif",
    28                                 R.id.imageView4);
    29                 loadImage3("http://images.cnblogs.com/logo_small.gif",
    30                                 R.id.imageView5);
    31         }
    32  
    33         private Handler handler = new Handler();
    34  
    35         private ExecutorService executorService = Executors.newFixedThreadPool(5);
    36  
    37         // 引入线程池来管理多线程
    38         private void loadImage3(final String url, final int id) {
    39                 executorService.submit(new Runnable() {
    40                         public void run() {
    41                                 try {
    42                                         final Drawable drawable = Drawable.createFromStream(
    43                                                         new URL(url).openStream(), "image.png");
    44                                         // 模拟网络延时
    45                                         SystemClock.sleep(2000);
    46                                         handler.post(new Runnable() {
    47                                                 public void run() {
    48                                                         ((ImageView) MainActivity.this.findViewById(id))
    49                                                                         .setImageDrawable(drawable);
    50                                                 }
    51                                         });
    52                                 } catch (Exception e) {
    53                                         throw new RuntimeException(e);
    54                                 }
    55                         }
    56                 });
    57         }
    58 }

    Handler+ExecutorService(线程池)+MessageQueue+缓存模式

    下面比起前一个做了几个改造:

    • 把整个代码封装在一个类中
    • 为了避免出现同时多次下载同一幅图的问题,使用了本地缓存
     1 package ghj1976.AndroidTest;
     2  
     3 import java.lang.ref.SoftReference;
     4 import java.net.URL;
     5 import java.util.HashMap;
     6 import java.util.Map;
     7 import java.util.concurrent.ExecutorService;
     8 import java.util.concurrent.Executors;
     9  
    10 import android.graphics.drawable.Drawable;
    11 import android.os.Handler;
    12 import android.os.SystemClock;
    13  
    14 public class AsyncImageLoader3 {
    15         // 为了加快速度,在内存中开启缓存(主要应用于重复图片较多时,或者同一个图片要多次被访问,比如在ListView时来回滚动)
    16         public Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();
    17          
    18         private ExecutorService executorService = Executors.newFixedThreadPool(5); // 固定五个线程来执行任务
    19         private final Handler handler = new Handler();
    20  
    21         /**
    22          * 
    23          * @param imageUrl
    24          *            图像url地址
    25          * @param callback
    26          *            回调接口
    27          * <a href="\"http://www.eoeandroid.com/home.php?mod=space&uid=7300\"" target="\"_blank\"">@return</a> 返回内存中缓存的图像,第一次加载返回null
    28          */
    29         public Drawable loadDrawable(final String imageUrl,
    30                         final ImageCallback callback) {
    31                 // 如果缓存过就从缓存中取出数据
    32                 if (imageCache.containsKey(imageUrl)) {
    33                         SoftReference<Drawable> softReference = imageCache.get(imageUrl);
    34                         if (softReference.get() != null) {
    35                                 return softReference.get();
    36                         }
    37                 }
    38                 // 缓存中没有图像,则从网络上取出数据,并将取出的数据缓存到内存中
    39                 executorService.submit(new Runnable() {
    40                         public void run() {
    41                                 try {
    42                                         final Drawable drawable = loadImageFromUrl(imageUrl); 
    43                                                  
    44                                         imageCache.put(imageUrl, new SoftReference<Drawable>(
    45                                                         drawable));
    46  
    47                                         handler.post(new Runnable() {
    48                                                 public void run() {
    49                                                         callback.imageLoaded(drawable);
    50                                                 }
    51                                         });
    52                                 } catch (Exception e) {
    53                                         throw new RuntimeException(e);
    54                                 }
    55                         }
    56                 });
    57                 return null;
    58         }
    59  
    60         // 从网络上取数据方法
    61         protected Drawable loadImageFromUrl(String imageUrl) {
    62                 try {
    63                         // 测试时,模拟网络延时,实际时这行代码不能有
    64                         SystemClock.sleep(2000);
    65  
    66                         return Drawable.createFromStream(new URL(imageUrl).openStream(),
    67                                         "image.png");
    68  
    69                 } catch (Exception e) {
    70                         throw new RuntimeException(e);
    71                 }
    72         }
    73  
    74         // 对外界开放的回调接口
    75         public interface ImageCallback {
    76                 // 注意 此方法是用来设置目标对象的图像资源
    77                 public void imageLoaded(Drawable imageDrawable);
    78         }
    79 }

    说明:

    final参数是指当函数参数为final类型时,你可以读取使用该参数,但是无法改变该参数的值。参看:Java关键字final、static使用总结 
    这里使用SoftReference 是为了解决内存不足的错误(OutOfMemoryError)的,更详细的可以参看:内存优化的两个类:SoftReference 和 WeakReference

    前段调用:

     1 package ghj1976.AndroidTest;
     2  
     3 import android.app.Activity;
     4 import android.graphics.drawable.Drawable;
     5 import android.os.Bundle;
     6  
     7 import android.widget.ImageView;
     8  
     9 public class MainActivity extends Activity {
    10         @Override
    11         public void onCreate(Bundle savedInstanceState) {
    12                 super.onCreate(savedInstanceState);
    13                 setContentView(R.layout.main);
    14                 loadImage4("http://www.baidu.com/img/baidu_logo.gif", R.id.imageView1);
    15                 loadImage4("http://www.chinatelecom.com.cn/images/logo_new.gif",
    16                                 R.id.imageView2);
    17                 loadImage4("http://cache.soso.com/30d/img/web/logo.gif",
    18                                 R.id.imageView3);
    19                 loadImage4("http://csdnimg.cn/www/images/csdnindex_logo.gif",
    20                                 R.id.imageView4);
    21                 loadImage4("http://images.cnblogs.com/logo_small.gif",
    22                                 R.id.imageView5);
    23         }
    24  
    25         private AsyncImageLoader3 asyncImageLoader3 = new AsyncImageLoader3();
    26  
    27         // 引入线程池,并引入内存缓存功能,并对外部调用封装了接口,简化调用过程
    28         private void loadImage4(final String url, final int id) {
    29                 // 如果缓存过就会从缓存中取出图像,ImageCallback接口中方法也不会被执行
    30                 Drawable cacheImage = asyncImageLoader3.loadDrawable(url,
    31                                 new AsyncImageLoader3.ImageCallback() {
    32                                         // 请参见实现:如果第一次加载url时下面方法会执行
    33                                         public void imageLoaded(Drawable imageDrawable) {
    34                                                 ((ImageView) findViewById(id))
    35                                                                 .setImageDrawable(imageDrawable);
    36                                         }
    37                                 });
    38                 if (cacheImage != null) {
    39                         ((ImageView) findViewById(id)).setImageDrawable(cacheImage);
    40                 }
    41         }
    42  
    43 }
  • 相关阅读:
    【Spring注解驱动开发】二狗子让我给他讲讲@EnableAspectJAutoProxy注解
    【Spring注解驱动开发】你敢信?面试官竟然让我现场搭建一个AOP测试环境!
    【Nginx】如何基于主从模式搭建Nginx+Keepalived双机热备环境?这是最全的一篇了!!
    华为分析+App Linking:一站式解决拉新、留存、促活难
    deeplink跳转快应用返回出现两次系统添加桌面的弹框
    4月26日19:30,#HMS Core Insights#直播系列第二期来了!
    HMS Toolkit自动化环境配置,助您高效集成HMS Core
    全体注意!一大波鸿蒙三方库即将到来
    HMS Toolkit助力开发者高效集成HMS Core
    教你使用一套代码解决出海问题
  • 原文地址:https://www.cnblogs.com/toosuo/p/2888971.html
Copyright © 2011-2022 走看看