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); ArrayListfutureTaskArrayList = new ArrayList (); 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 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{ 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如下: