Android线程池(一)——Executors(线程池)以及FutureTask使用示例

MainActivity如下:
package cc.vv;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import android.os.Bundle;
import android.app.Activity;
/**
 * Demo描述:
 * 线程池使用示例
 * 
 * 创建线程池的主要方式:
 * newCachedThreadPool()
 * newFixedThreadPool(int i)
 * newScheduledThreadPool(int i)
 * SingleThreadExecutor()
 * 
 * Demo内容:
 * 1 newFixedThreadPool(int i)和SingleThreadExecutor()的使用
 * 2 线程池(Executors)和FutureTask的结合使用
 * 
 * 参考资料:
 * 1 http://blog.csdn.net/ns_code/article/details/17465497
 * 2 http://blog.csdn.net/tounaobun/article/details/8586675
 * 3 http://blog.csdn.net/linghu_java/article/details/17123057
 * 4 http://blog.csdn.net/andycpp/article/details/8902655
 *   Thank you very much
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//testExecutors1();
		//testExecutors2();
		testExecutors3();
	}

	////////////////////////////////////////////////////////////////////////
	/**
	 * newFixedThreadPool的使用
	 * 1 创立一个线程池,该线程池中有5个线程
	 * 2 将15个RunnableImpl对象放入线程中执行
	 *  可以看到这15个RunnableImpl在5个线程中调用
	 * 
	 */
	private void testExecutors1(){
		Executor executor=Executors.newFixedThreadPool(5);
		RunnableImpl1 runnableImpl1=null;
		for (int i = 0; i < 15; i++) {
			runnableImpl1=new RunnableImpl1();
			executor.execute(runnableImpl1);
		}
	}
	
	private class RunnableImpl1 implements Runnable{
		@Override
		public void run() {
			System.out.println("线程名字:"+Thread.currentThread().getName());
		}
		
	}
   ////////////////////////////////////////////////////////////////////////
	
	
	
	
   ////////////////////////////////////////////////////////////////////////
	/**
	 * newSingleThreadExecutor()的使用
	 * 创建一个只包含了一个线程的线程池,它只会用该唯一工作线程来执行任务.
	 * 从而保证所有任务按照指定顺序(FIFO)执行.
	 * 
	 * 该特点还是很具有实际意义的.
	 * 
	 */
	private void testExecutors2(){
		Executor executor=Executors.newSingleThreadExecutor();
		RunnableImpl2 runnableImpl2=null;
		for (int i = 0; i < 15; i++) {
			runnableImpl2=new RunnableImpl2(""+i);
			executor.execute(runnableImpl2);
		}
	}
	
	private class RunnableImpl2 implements Runnable{
		private String name;
		private RunnableImpl2(String name){
			this.name=name;
		}
		@Override
		public void run() {
			System.out.println("线程名字:"+name);
		}
		
	}
	
   ////////////////////////////////////////////////////////////////////////
	
	
	
	
	////////////////////////////////////////////////////////////////////////
	/**
	 * 线程池(Executors)和FutureTask的结合使用
	 */
	private void testExecutors3() {
		ExecutorService executorService = Executors.newFixedThreadPool(5);
		ArrayList<FutureTask> futureTaskArrayList = new ArrayList<FutureTask>();
		CallableImpl callableImpl = null;
		FutureTask futureTask = null;
		for (int i = 0; i < 10; i++) {
			callableImpl = new CallableImpl();
			futureTask = new FutureTask(callableImpl);
			//执行FutureTask
			executorService.execute(futureTask);
			//将每个FutureTask保存到集合中,便于之后获取其对应的结果.
			futureTaskArrayList.add(futureTask);
		}

		//遍历集合,获取每个FutureTask执行的结果
		try {
			for (Iterator<FutureTask> iterator = futureTaskArrayList.iterator(); iterator.hasNext();) {
				FutureTask ft = (FutureTask) iterator.next();
				//直到该FutureTask计算完成,才会调用到ft.get()
				while (!ft.isDone());
				System.out.println("-----> 返回结果:" + ft.get());
			}
		} catch (Exception e) {
			
		}

	}

	// //////////////////////////////////////////////////////////////////////
	
	
	
	
}

CallableImpl如下:

package cc.vv;

import java.util.Random;
import java.util.concurrent.Callable;

public class CallableImpl implements Callable<Integer> {
	
	public CallableImpl() {}

	@Override
	public Integer call() throws Exception {
		int result=new Random().nextInt(100);
		System.out.println(""+Thread.currentThread().getName());
		return Integer.valueOf(result);
	}

}


main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


你可能感兴趣的:(Android线程池(一)——Executors(线程池)以及FutureTask使用示例)