Android-ThreadPool

Executors.newFixedThreadPool(5);



具体代码如下

mainfest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.inctech.hello"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    
	<uses-permission android:name="android.permission.INTERNET"></uses-permission>
	
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 

</manifest>

layout.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

   

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="20dp"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

    <ImageView
        android:id="@+id/iv2"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignLeft="@+id/iv1"
        android:layout_below="@+id/iv1"
        android:layout_marginTop="20dp"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

    <ImageView
        android:id="@+id/iv3"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignLeft="@+id/iv1"
        android:layout_below="@+id/iv2"
        android:layout_marginTop="20dp"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

    <ImageView
        android:id="@+id/iv4"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignLeft="@+id/iv1"
        android:layout_below="@+id/iv3"
        android:layout_marginTop="20dp"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

    <ImageView
        android:id="@+id/iv5"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignLeft="@+id/iv1"
        android:layout_below="@+id/iv4"
        android:layout_marginTop="20dp"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Btn" />

</RelativeLayout>


Java code

package com.inctech.hello;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private Button bt;  
    private static Handler mainHandler = new Handler(){  
        @Override  
        public void handleMessage(Message msg) {  
            // TODO Auto-generated method stub  
            super.handleMessage(msg);    
        }  
    };  
    
    ExecutorService service = Executors.newFixedThreadPool(5);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }
    
//    private void testThread0() {
//    	Handler handler = new Handler();
//    	for (int i = 0; i < 3; i++) {
//	    	handler.post(new Runnable() {
//	    		@Override
//	    		public void run() {
//	    			Log.i("testThread0", Thread.currentThread().getName());
//	    		}
//	    	});
//    	}
//    	handler = null;
//    }
    
    private void initViews(){       
        bt = (Button)findViewById(R.id.bt);  
        bt.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/23c1625aca99f02c50d8e510383a34e7.jpg",R.id.iv1);  
                loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/c4698d97ef6d10722c8e917733c7beb3.jpg",R.id.iv2);  
                loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/f332ffe433be2a3112be15f78bff5a40.jpg",R.id.iv3);  
                loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/6ff8a9c647a1e80bc602eeda48865d4c.jpg",R.id.iv4);  
                loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/f104d069f7443dca52a878d779392874.jpg",R.id.iv5);  
            }  
        });  
    }
    
    private void loadImagesByExecutors(final String url,final int id){  
        service.submit(new Runnable(){  
            @Override  
            public void run() {  
                Log.i("当前线程:", ""+Thread.currentThread().getName());  
                  
                try {  
                    final Drawable drawable  = Drawable.createFromStream(new URL(url).openStream(), "image.gif");  
                    mainHandler.post(new Runnable(){  
                        @Override  
                        public void run() {//这将在主线程运行  
                            ((ImageView)MainActivity.this.findViewById(id)).setImageDrawable(drawable);  
                        }  
                    });  
                      
                } catch (MalformedURLException e) {  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }   
            }         
        });     
    }  
}

参考原文是

http://blog.csdn.net/jie1991liu/article/details/16961701


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值