1. The disadvantage of new thread
Recent projects have always used threads, and because of the habitual use of thread and handler, but after browsing some articles on the Internet blog after the discovery. The original new thread is also a lot of drawbacks. Is your code the same as the following?
New Thread (new Runnable () { @override publicvoid run () { //
Is this what you do every time you perform an asynchronous task? So, you is out,so do I. I found a few drawbacks to the new thread:
1, each time to new thread, a new object, resulting in poor performance of the object.
2, the thread lacks the unified management, may the unrestricted new thread, competes the system resources with each other, causes the system resources which consumes excessively, finally caused the freezing or is oom.
3, lack of more functions, such as timed execution, regular execution, thread interruption.
The benefits of the four thread pools provided by Java compared to the new thread are:
① can reuse existing threads, reduce object creation, extinction, and high performance.
② effectively control the number of concurrent threads, improve the utilization rate of system resources, avoid excessive competition for system resources, resulting in congestion.
③ provides functions such as timed execution, periodic execution, single-threaded, concurrency control, and more.
2. Java thread pool
Java provides four thread pools via executors
Creates a cacheable thread pool that can flexibly reclaim idle threads if the thread pool length exceeds the processing needs, such as a new thread without recycling
Executorservice Cachedthreadpool =Executors.newcachedthreadpool (); for(inti=0; i<10;i++){ Final intindex=i; Try{thread.sleep (index*1000); }Catch(interruptedexception e) {e.printstacktrace (); } cachedthreadpool.execute (NewRunnable () {@override Public voidrun () {System.out.println (index); } }); }
The thread pool is infinitely large, and when the first task is completed when the second task is performed, the first task's threads are reused instead of each new thread.
Creates a thread pool that controls the maximum number of concurrent threads, and the excess threads wait in the queue
Executorservice Fixedthreadpool = Executors.newfixedthreadpool (3); for(inti = 0; I < 10; i++) { Final intindex =i; Fixedthreadpool.execute (NewRunnable () {@Override Public voidrun () {Try{System.out.println (index); Thread.Sleep (2000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } });}
Because the thread size is 3, each task outputs index and sleeps for 2 seconds, so 3 digits are printed every two seconds
Create a fixed-line pool to support timed and recurring task execution
Scheduledexecutorservice Scheduledthreadpool = Executors.newscheduledthreadpool (5); Scheduledthreadpool.schedule (new Runnable () { @Override publicvoid Run () { System.out.println ("Delay 3 seconds"); 3, Timeunit.seconds);
This is expressed as a delay of 3 seconds to execute
Scheduledthreadpool.scheduleatfixedrate (new Runnable () { @Override public void Run () { System.out.println ("Delay 1 seconds, and excute every 3 seconds"); 1, 3, timeunit.seconds);
This representation is performed every 3 seconds after a delay of 1 seconds
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
Executorservice Singlethreadexecutor =executors.newsinglethreadexecutor (); for(inti = 0; I < 10; i++) { Final intindex =i; Singlethreadexecutor.execute (NewRunnable () {@Override Public voidrun () {Try{System.out.println (index); Thread.Sleep (2000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } });}
The results are output sequentially, which is equivalent to performing each task sequentially.
Well, see here, whether you have the impulse to abandon the use of new thread. Anyway, I am the heart, the heartbeat than action, move up, everybody!!!!
Android thread pool