1. The disadvantage of new thread
Perform an asynchronous task are you still just following the new thread?
[Java]View Plaincopy
- New Thread (new Runnable () {
- @Override
- public Void Run () {
- //TODO auto-generated method stub
- }
- }). 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:
[Java]View Plaincopy
- Executorservice Cachedthreadpool = Executors.newcachedthreadpool ();
- for (int i = 0; i < i++) {
- final int index = i;
- try {
- Thread.Sleep (Index * 1000);
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- Cachedthreadpool.execute (new Runnable () {
- @Override
- public Void Run () {
- SYSTEM.OUT.PRINTLN (index);
- }
- });
- }
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:
[Java]View Plaincopy
- Executorservice Fixedthreadpool = Executors.newfixedthreadpool (3);
- for (int i = 0; i < i++) {
- final int index = i;
- Fixedthreadpool.execute (new Runnable () {
- @Override
- public Void Run () {
- try {
- SYSTEM.OUT.PRINTLN (index);
- Thread.Sleep (2000);
- } catch (Interruptedexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- });
- }
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:
[Java]View Plaincopy
- Scheduledexecutorservice Scheduledthreadpool = Executors.newscheduledthreadpool (5);
- Scheduledthreadpool.schedule (new Runnable () {
- @Override
- public Void Run () {
- SYSTEM.OUT.PRINTLN ("delay 3 seconds");
- }
- }, 3, timeunit.seconds);
Represents a delay of 3 seconds for execution.
Regular code execution is as follows:
[Java]View Plaincopy
- Scheduledthreadpool.scheduleatfixedrate (new Runnable () {
- @Override
- public Void Run () {
- SYSTEM.OUT.PRINTLN ("Delay 1 seconds, and excute every 3 seconds");
- }
- }, 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:
[Java]View Plaincopy
- Executorservice singlethreadexecutor = Executors.newsinglethreadexecutor ();
- for (int i = 0; i < i++) {
- final int index = i;
- Singlethreadexecutor.execute (new Runnable () {
- @Override
- public Void Run () {
- try {
- SYSTEM.OUT.PRINTLN (index);
- Thread.Sleep (2000);
- } catch (Interruptedexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- });
- }
The results are output sequentially, which is equivalent to performing each task sequentially.
Single threading is useful in some cases, and most GUI programs are single-threaded. The use of single threading in Android for performance tuning is described later.
Java (Android) thread pool