您的位置:首页 > 移动开发 > Android开发

Android-ThreadPool

2015-09-25 16:54 441 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: