1. The disadvantage of new thread
Perform an asynchronous task are you still just following the new thread?
1 Newthread (newrunnable () {2 3 @Override4 Publicvoidrun () {5 // TODO auto-generated method stub6 } 7 }). Start ();
Then you are out too much, and the drawbacks of new thread are as follows:
A. Each new thread creates a poor performance for the newly created object.
B. Lack of unified management of threads, the possibility of unlimited new threads, competing with each other, and potentially consuming excessive system resources can lead to freezing or oom.
C. Lack of additional functionality, such as timed execution, periodic execution, and thread interruption.
The benefits of the four thread pools provided by new Thread,java are:
A. Reusing existing threads, reducing the cost of object creation, extinction, and performance.
B. Can effectively control the maximum number of concurrent threads, improve the utilization of system resources, while avoiding excessive resource competition, avoid clogging.
C. Provide functions such as timed execution, periodic execution, single-threaded, concurrency control, etc.
2. Java thread Pool
Java provides four thread pools through executors, namely:
Newcachedthreadpool creates a cacheable thread pool that can flexibly reclaim idle threads if the thread pool length exceeds the processing needs, and creates a new thread if it is not recyclable.
Newfixedthreadpool creates a thread pool that controls the maximum number of concurrent threads, and the excess threads wait in the queue.
Newscheduledthreadpool creates a fixed-line pool that supports timed and recurring task execution.
Newsinglethreadexecutor creates a single threaded thread pool that performs tasks with only a single worker thread, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority).
(1). Newcachedthreadpool
Creates a cacheable thread pool that can flexibly reclaim idle threads if the thread pool length exceeds the processing needs, and creates a new thread if it is not recyclable. The sample code is as follows:
1Executorservice cachedthreadpool=Executors.newcachedthreadpool ();2 for(inti=0;i<10;i++){3finalintindex=i;4 Try{5Thread.Sleep (index*1000);6}Catch(interruptedexceptione) {7 e.printstacktrace ();8 }9 Ten Cachedthreadpool.execute (newrunnable () { One A @Override - Publicvoidrun () { - System.out.println (index); the } - }); -}
The thread pool is infinitely large, and when the first task is completed when the second task is performed, the threads that perform the first task are reused instead of each new thread.
(2). Newfixedthreadpool
Creates a thread pool that controls the maximum number of concurrent threads, and the excess threads wait in the queue. The sample code is as follows:
1Executorservice Fixedthreadpool=executors.newfixedthreadpool (3);2 for(inti=0;i<10;i++){3finalintindex=i;4 Fixedthreadpool.execute (newrunnable () {5 6 @Override7 Publicvoidrun () {8 Try{9 System.out.println (index);TenThread.Sleep (2000); One}Catch(interruptedexceptione) { A //TODO auto-generated Catch block - e.printstacktrace (); - } the } - }); -}
Because the thread pool size is 3, each task outputs index after sleep for 2 seconds, so 3 digits are printed every two seconds.
The size of a fixed-length pool is best set based on system resources. such as Runtime.getruntime (). Availableprocessors (). Refer to Preloaddatacache.
(3) Newscheduledthreadpool
Create a fixed-line pool that supports timed and recurring task execution. The deferred execution sample code is as follows:
1 scheduledexecutorservice scheduledthreadpool=executors.newscheduledthreadpool (5); 2 Scheduledthreadpool.schedule (newrunnable () {3 4 @Override5 Publicvoidrun () {6 System.out.println ("Delay 3 seconds"); 7 }8 },3,timeunit.seconds);
Represents a delay of 3 seconds for execution.
The sample code is executed periodically as follows:
1 scheduledthreadpool.scheduleatfixedrate (newrunnable () {2 3 @Override 4 Publicvoidrun () {5 System.out.println ("Delay 1 seconds, and Excute Every 3 seconds "); 6 }7 },1,3,timeunit.seconds);
Represents a delay of 1 seconds after every 3 seconds.
Scheduledexecutorservice is safer and more powerful than a timer, and there is a separate comparison in the back.
(4), Newsinglethreadexecutor
Creates a single threaded thread pool that performs tasks with only a single worker thread, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority). The sample code is as follows:
1Executorservice singlethreadexecutor=executors.newsinglethreadexecutor ();2 for(inti=0;i<10;i++){3finalintindex=i;4 Singlethreadexecutor.execute (newrunnable () {5 6 @Override7 Publicvoidrun () {8 Try{9 System.out.println (index);TenThread.Sleep (2000); One}Catch(interruptedexceptione) { A //TODO auto-generated Catch block - e.printstacktrace (); - } the } - }); -}
The results are output sequentially, which is equivalent to performing each task sequentially.
Most of the current GUI programs are single-threaded. Single-threaded Android can be used for database operations, file operations, application batch installation, application bulk deletion, etc. that are not suitable for concurrency but may have IO blocking and affect UI thread response actions.