Java concurrency (1)-executors
There are two common ways to create multithreading in Java, I will not say much, is to achieve runnable and inherit thread. So let's talk about other ways now.
What is executors?
Executors, which we call the executor, manages the thread object and simplifies the concurrency process, which provides an indirect layer between the client and the task execution. The task is performed by an intermediary. There is no need to manage the life cycle of threads.
We first implement a runnable class, which is very simple, it is in a counter, and for each created object, its ID is different, convenient for us to identify.
Class Mycountdownrunnale implements Runnable {
private int countdown = 10;
private static int taskcount = 0;
private final int id = taskcount + +;
Public Mycountdownrunnale () {}
Public Mycountdownrunnale (int countdown) {
This.countdown = Countdown;
}
@Override
public void Run () {
while (Countdown--> 0) {
System.out.print (ToString ());
Thread.yield ();//threads all give up the right to join the competition.
}
}
@Override
Public String toString () {
Return "$" + ID + "<" + Thread.CurrentThread (). GetId () + "> (" + (Countdown > 0?) Countdown: "Zero") + "),";
}
}
Cachedthreadpool Way
The following code:
Executorservice exec = Executors.newcachedthreadpool ();
for (int i = 0; i < 5; i + +) {
Exec.execute (new Mycountdownrunnale ());
}
Exec.shutdown ();//Prevent new thread from committing
execution result:
$1<12> (9), $3<14> (9), $1<12> (8), $2<13> (9), $0<11> (9),
$2<13> (8), $1< 12> (7), $3<14> (8), $4<15> (9), $3<14> (7),
$1<12> (6), $2<13> (7), $0<11> (8), $2<13> (6), $1<12> (5),
$3<14> (6), $4<15> (8), $3<14> (5), $1<12> (4), $2<13 > (5),
$3<14> (4), $2<13> (4), $0<11> (7), $2<13> (3), $3<14> (3),
$1<12> (3 ), $4<15> (7), $1<12> (2), $3<14> (2), $2<13> (2),
$0<11> (6), $2<13> (1), $3<14 > (1), $2<13> (zero), $1<12> (1),
$4<15> (6), $1<12> (zero), $3<14> (zero), $0<11 > (5), $4<15> (5),
$0<11> (4), $4<15> (4), $0<11> (3), $4<15> (3), $0<11> (2),
$4<15> (2), $0<11> (1), $4<15> (1), $0<11> (zero), $4<15> (zero),
The Fixedthreadpool method
is the following code:
Executorservice exec =executors.newfixedthreadpool (4);
for (int i = 0; i < 5; i + +) {
Exec.execute (new Mycountdownrunnale ());
}
Exec.shutdown ();//Prevent new thread from committing
execution result:
$0<11> (9), $3<14> (9), $2<13> (9), $1<12> (9), $2<13> (8),
$3<14> (8), $0< 11> (8), $3<14> (7), $0<11> (7), $2<13> (7),
$1<12> (8), $2<13> (6), $0<11> (6), $3<14> (6), $0<11> (5),
$2<13> (5), $1<12> (7), $2<13> (4), $0<11> (4), $3<14 > (5),
$0<11> (3), $2<13> (3), $1<12> (6), $2<13> (2), $3<14> (4),
$2<13> (1 ), $0<11> (2), $1<12> (5), $0<11> (1), $2<13> (zero),
$3<14> (3), $0<11> (zero), $ <12> (4), $1<12> (3), $3<14> (2),
$1<12> (2), $4<13> (9), $1<12> (1), $1<12> (zero), $3<14> (1),
$4<13> (8), $3<14> (zero), $4<13> (7), $4<13> (6), $4<13> (5),
$4<13> (4), $4<13> (3), $4<13> (2), $4<13> (1), $4<13> (Zero),
The Singlethreadexecutor method
is the following code:
Executorservice exec = Executors.newsinglethreadexecutor ();
for (int i = 0; i < 5; i + +) {
Exec.execute (New Mycountdownrunnale ());
}
Exec.shutdown ();//Prevent new thread submissions
Execution Result:
$0<11> (9), $0<11> (8), $0<11> (7), $0<11> (6), $0<11> (5),
$0<11> (4), $0<11> (3), $0<11> (2), $0<11> (1), $0<11> (Zero),
$1<11> (9), $1<11> (8), $1<11> (7), $1<11> (6), $1<11> (5),
$1<11> (4), $1<11> (3), $1<11> (2), $1<11> (1), $1<11> (Zero),
$2<11> (9), $2<11> (8), $2<11> (7), $2<11> (6), $2<11> (5),
$2<11> (4), $2<11> (3), $2<11> (2), $2<11> (1), $2<11> (Zero),
$3<11> (9), $3<11> (8), $3<11> (7), $3<11> (6), $3<11> (5),
$3<11> (4), $3<11> (3), $3<11> (2), $3<11> (1), $3<11> (Zero),
$4<11> (9), $4<11> (8), $4<11> (7), $4<11> (6), $4<11> (5),
$4<11> (4), $4<11> (3), $4<11> (2), $4<11> (1), $4<11> (Zero),
The difference between three people
Looking at three different execution results, I think we all know that the difference between 1 and 2 is 3.
1, the third way, such as its name, single-threaded run, he will submit the task, in order to execute, but also in the same thread execution.
2. What is the difference between the first and the second? Java Training Technology The parameters of the construction method are different, the second way of the parameters means that a one-time pre-implementation of expensive thread allocation, then can be used directly, but can not be abused, because the number is limited. The first is that the execution process typically creates the required number of threads and then recycles the old thread, stopping the creation of the thread.
3, they two apart from this distinction, there is also an important feature is that fixedthreadpool guaranteed that "no two threads" will be called concurrently.
4. If a thread instance requests to join Fixedthreadpool, if the instance does not exist and the number of thread pools is not reached, an instance is created, otherwise the wait sequence is first added, and when a thread in Fixedthreadpool stops and moves the pool, Thread instances to join the thread pool.
5, Cachedthreadpool thread instance the default timeout is 60s, more than this time, the threading instance is stopped and moved out of the Cachedthreadpool for short-lived, asynchronous thread tasks. The Fixedthreadpool has no timeout mechanism for stable and concurrent thread tasks. In this regard, you can see the source code Executors.java:
As follows, the third parameter is the time-out setting. 0 is not timed out.
public static Executorservice Newcachedthreadpool () {
return new Threadpoolexecutor (0, Integer.max_value,
60L, Timeunit.seconds,
New synchronousqueue<runnable> ());
}
public static Executorservice newfixedthreadpool (int nthreads) {
return new Threadpoolexecutor (Nthreads, Nthreads,
0L, Timeunit.milliseconds,
New linkedblockingqueue<runnable> ());
}
Java Concurrency Executors Learning notes