Executors(线程池)以及FutureTask使用示例
来源:程序员人生 发布时间:2014-11-07 08:56:09 阅读次数:2312次
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 创建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()的使用
* 创建1个只包括了1个线程的线程池,它只会用该唯1工作线程来履行任务.
* 从而保证所有任务依照指定顺序(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>
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠