Android thread pool (1) -- Executors (thread pool) and FutureTask usage example
MainActivity is 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 example ** main method for creating a thread pool: * newCachedThreadPool () * newFixedThreadPool (int I) * newScheduledThreadPool (int I) * SingleThreadExecutor () ** Demo content: * 1 Use of newFixedThreadPool (int I) and SingleThreadExecutor () * 2 Use of Thread Pool (Executors) and FutureTask ** references: * 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 {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // testExecutors1 (); // testExecutors2 (); testExecutors3 ();} //////////////////////////////////////// /// // /// *** use of newFixedThreadPool * 1. Create a thread pool, there are 5 threads in the thread pool * 2 put 15 RunnableImpl objects into the thread for execution * you can see that the 15 RunnableImpl calls **/private void testExecutors1 () in 5 threads () {Executor executor = Executors. newFixedThreadPool (5); RunnableImpl1 runnableImpl1 = null; for (int I = 0; I <15; I ++) {runnableImpl1 = new datagcute (runnableImpl1 );}} private class RunnableImpl1 implements Runnable {@ Overridepublic void run () {System. out. println ("Thread name:" + Thread. currentThread (). getName ());}} //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /// // *** newSingleThreadExecutor () use * to create a thread pool that only contains one thread. It only uses this unique working thread to execute the task. * This ensures that all tasks are executed in the specified sequence (FIFO. ** this feature is of practical significance. **/private void testExecutors2 () {Executor executor = Executors. newSingleThreadExecutor (); RunnableImpl2 runnableImpl2 = null; for (int I = 0; I <15; I ++) {Signature = new runnableImpl2 ("invalid cute (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) use together with FutureTask */private void testExecutors3 () {ExecutorService executorService = Executors. newFixedThreadPool (5); ArrayList
FutureTaskArrayList = new ArrayList
(); CallableImpl callableImpl = null; FutureTask futureTask = null; for (int I = 0; I <10; I ++) {callableImpl = new CallableImpl (); futureTask = new FutureTask (callableImpl); // execute futuretaskexecutorservice.exe cute (futureTask); // save each FutureTask to the set for later obtaining the corresponding result. futureTaskArrayList. add (futureTask);} // traverses the set to obtain the execution result of each FutureTask. try {for (Iterator
Iterator = futureTaskArrayList. iterator (); iterator. hasNext ();) {FutureTask ft = (FutureTask) iterator. next (); // it will not be called to ft until the calculation of the FutureTask is completed. get () while (! Ft. isDone (); System. out. println ("-----> return result:" + ft. get () ;}} catch (Exception e) {}}///////////////////////////////////// ///////////////////////////////////}
CallableImpl is as follows:
package cc.vv;import java.util.Random;import java.util.concurrent.Callable;public class CallableImpl implements Callable
{public CallableImpl() {}@Overridepublic Integer call() throws Exception {int result=new Random().nextInt(100);System.out.println(""+Thread.currentThread().getName());return Integer.valueOf(result);}}
Main. xml is as follows: