1. singlethreadexecutor is like a fixedthreadpool with a thread number of 1.
2. If multiple tasks are submitted to singlethreadexecutor, these tasks are queued. The output result shows that the task is executed in the submission order.
3. singlethreadexecutor serializes all submitted tasks, and maintains its own (hidden) hanging task queue .??? (Do not understand)
4. singlethreadexecutor ensures that only a unique task is running in any thread. (When multiple threads use the same file system, singlethreadexecutor can be used for synchronization)
Import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; public class singlethreadexecutor {public static void main (string [] ARGs) {executorservice = executors. newsinglethreadexecutor (); For (INT I = 0; I <5? I =={executorservice.exe cute (New liftoff ();} executorservice. shutdown ();} static public class liftoff implements runnable {protected int countdown = 10; private stat IC int taskcount = 0; private final int id = taskcount ++; Public liftoff () {} public liftoff (INT countdown) {This. countdown = Countdown;} Public String status () {return "#" + ID + "(" + (Countdown> 0? Countdown: "liftoff! ") +"), ";} Public void run () {While (countdown --> 0) {system. out. print (status (); thread. yield () ;}}}// output // #0 (9), #0 (8), #0 (7), #0 (6 ), #0 (5), #0 (4), #0 (3), #0 (2), #0 (1), #0 (liftoff !), // #1 (9), #1 (8), #1 (7), #1 (6), #1 (5), #1 (4 ), #1 (3), #1 (2), #1 (1), #1 (liftoff !), // #2 (9), #2 (8), #2 (7), #2 (6), #2 (5), #2 (4 ), #2 (3), #2 (2), #2 (1), #2 (liftoff !), // #3 (9), #3 (8), #3 (7), #3 (6), #3 (5), #3 (4 ), #3 (3), #3 (2), #3 (1), #3 (liftoff !), // #4 (9), #4 (8), #4 (7), #4 (6), #4 (5), #4 (4 ), #4 (3), #4 (2), #4 (1), #4 (liftoff !),
Use of singlethreadexecutor