Mainactivity as follows:
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 Description: * Thread Pool Use example * * The main way to create a thread pool: * Newcachedthreadpool () * Newfixedthreadpool (int i) * Newscheduledthreadpool (int i) * Singlethreadexecutor () * * Demo Content: * 1 newfixedthreadpool (int i) and singlethreadexecutor () use * 2 thread pool (executors) and futuretask combination * * Reference: * 1 htt p://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 {@Overrideprotected void onCreate (Bundle save Dinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);//testexecutors1 ();//testExecutors2 (); TestExecutors3 ();} * * Newfixedthreadpool Use * 1 Create a thread pool, The thread pool has 5 threads * 2 executes 15 Runnableimpl objects into the thread * you can see that 15 Runnableimpl are called in 5 threads * */private void TestExecutors1 () {Executor Execu Tor=executors.newfixedthreadpool (5); RUNNABLEIMPL1 runnableimpl1=null;for (int i = 0; i <; i++) {runnableimpl1=new RunnableImpl1 (); Executor.execute (Runn ABLEIMPL1);}} Private class RUNNABLEIMPL1 implements runnable{@Overridepublic void Run () {System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName ());}} //////////////////////////////////////////////////////////////////////// /////////////////////////////////////// * * Newsinglethreadexecutor () use * Create a thread pool that contains only one threads, and it will only use that unique worker thread to perform the task. * This ensures that all tasks are executed in the specified order (FIFO). * * This feature is still very practical. * */private void TestExecutors2 () {Executor executor=executors.newsinglethreadexecutor (); RUNNABLEIMPL2 runnableimpl2=null;for (int i = 0; i <; i++) {RunNableimpl2=new RunnableImpl2 ("" +i); Executor.execute (RUNNABLEIMPL2);}} Private class RUNNABLEIMPL2 implements Runnable{private string Name;private RunnableImpl2 (string name) {This.name=name;} @Overridepublic void Run () {System.out.println ("Thread Name:" +name);}} /////////////////////////////////////////////////////////////////////////////////////////////////////////////// * * thread pool (executors) and Futuretask are combined using */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 <; i++) {Callableimpl = new Callableimpl (); futuretask = new Futuretas K (Callableimpl);//execute Futuretaskexecutorservice.execute (futuretask);//Save each Futuretask to the collection, It facilitates subsequent acquisition of its corresponding results. Futuretaskarraylist.add (futuretask);} Iterate through the collection to get the result of each futuretask execution try {for (iterator<futuretask> Iterator = FuturetaskarrayList.iterator (); Iterator.hasnext ();) {Futuretask ft = (futuretask) iterator.next ();//until the Futuretask calculation is complete, it will be called to Ft.get () while (!ft.isdone ()); SYSTEM.OUT.PRINTLN ("-----> Return Result:" + ft.get ());}} catch (Exception e) {}}////////////////////////////////////////////////////////////////////////}
Callableimpl as follows:
Package Cc.vv;import Java.util.random;import Java.util.concurrent.callable;public class Callableimpl implements callable<integer> {public Callableimpl () {} @Overridepublic an Integer call () throws Exception {int result=new Random ( ). Nextint (100); System.out.println ("" +thread.currentthread (). GetName ()); return integer.valueof (Result);}}
Main.xml as follows:
<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 thread pool (i)--executors (thread pool) and Futuretask use example