Executors compared Thread

1. Thread new drawbacks as follows:
. A difference between each new Thread New Object properties.
b. Thread the lack of unified management, unlimited possible new thread, compete with each other, and may take up too many system resources lead to crashes.
c. the lack of additional features, such as scheduled execution, periodic execution, the thread is interrupted.

 

2. Executors advantages:
A reuse existing threads, reducing object creation, the demise of the cost, good performance.
b. can effectively control the maximum number of concurrent threads, improve utilization of system resources, while avoiding excessive competition for resources, to avoid clogging.
c. Provide a timing executed to perform regular, single-threaded, concurrent control.
(1). NewCachedThreadPool
create a cacheable thread pool, if the thread pool is longer than the processing needs, flexibility recovered idle thread, if not recovered, the new thread. The following sample code:
ExecutorService cachedThreadPool Executors.newCachedThreadPool = ();
cachedThreadPool.execute (the Runnable new new () {
        @Override
        public void RUN () {
            System.out.println (index);
        }
    });
thread pool is infinite, when the second task is executed first task has been completed, the thread used to perform the first task will be complex, rather than each time a new thread.
(2). NewFixedThreadPool
create a fixed-size thread pool, you can control the maximum number of concurrent threads, excess threads will wait in the queue.
ExecutorService fixedThreadPool = Executors.newFixedThreadPool (3);
(3) newScheduledThreadPool
create a fixed-size thread pool to support regular and periodic task execution. Delayed execution the following sample code:
The ScheduledExecutorService scheduledThread

Guess you like

Origin blog.csdn.net/qq_38998213/article/details/104802490