Java thread pool (newSingleThreadExecutor () for small applications and java Thread Pool
Create a single thread to operate an unbounded queue task without using additional threads. If the thread crashes, a new one will be created until the task is completed.
Code:
Import java. util. concurrent. executorService; import java. util. concurrent. executors; public class Threadd2 {public static void main (String [] args) {ExecutorService es = Executors. newSingleThreadExecutor (); run command = new run (); es.exe cute (command); es. shutdown () ;}} class run implements Runnable {@ Override public void run () {// TODO Auto-generated method stub try {Thread. sleep (100);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println (Thread. currentThread (). getName ());}}
Output result: pool-1-thread-1
Pool-1-thread-1