1: the first to write a Runnable.
. 1 Package the ThreadPool; 2 . 3 / ** . 4 * @ProjectName: smartdata . 5 * @package: the ThreadPool . 6 * @ClassName: TicketRunnable . 7 * @author: heluwei . 8 * @Description: . 9 * @date: 2020/3/23 18 is: 55 10 * @Version: 1.0 . 11 * / 12 is public class TicketRunnable the implements the Runnable { 13 is 14 // for consistency votes, static votes to 15 static int the tick = 20 is ; 16 // create a static key 17 static Object OB = "AA"; // values are arbitrary 18 // override the run method, to achieve operation ticket . 19 @Override 20 is public void run () { 21 is the while (the tick> 0 ) { 22 is the synchronized (OB) { // this is very important, you must use a lock 23 // go people are going to key in your hand, take the keys out after let out 24- IF (tick> 0 ) { 25 System.out.println (the Thread. . currentThread () getName () + " sold on" + tick + "tickets" ); 26 is tick-- ; 27 } the else{ 28 System.out.println ( "sold out" ); 29 } 30 } 31 is the try { 32 the Thread.sleep (1000); // rest one second 33 is } the catch (InterruptedException E) { 34 is e.printStackTrace (); 35 } 36 } 37 } 38 is 39 }
Two: Use thread pool operation
1 package ThreadPool; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.concurrent.ArrayBlockingQueue; 6 import java.util.concurrent.ThreadPoolExecutor; 7 import java.util.concurrent.TimeUnit; 8 9 /** 10 * @ProjectName: smartdata 11 * @Package: ThreadPool 12 * @ClassName: ThreadPoolExecutorDemo 13 * @Author: heluwei 14 * @Description: ThreadPoolExecutor线程池 15 * @Date: 2020/3/21 20:35 16 * @Version: 1.0 . 17 * / 18 is public class ThreadPoolExecutorDemo { . 19 Private static Final int CORE_POOL_SIZE =. 5; // core number of threads. 5 20 is Private static Final int MAX_POOL_SIZE = 10; // Maximum number of threads 10 21 is Private static Final int = 100 QUEUE_CAPACITY; // 22 is Private static Final long KEEP_ALIVE_TIME = 1L; // when the number of the core threads is greater than the number of threads, the excess idle threads longest surviving 23 is 24 public static void main (String ... args) { 25 // way to use Alibaba recommended creating a thread pool 26 // custom parameters created by the constructor ThreadPoolExecutor 27 ThreadPoolExecutor Executor = new new ThreadPoolExecutor ( 28 CORE_POOL_SIZE, 29 MAX_POOL_SIZE, 30 KEEP_ALIVE_TIME, // when the number of the core threads is greater than the number of threads, the excess idle threads longest surviving 31 is TimeUnit.SECONDS, // time unit 32 new new ArrayBlockingQueue with <> (QUEUE_CAPACITY), // task queue for storing tasks awaiting execution queue 33 is new new ThreadPoolExecutor.CallerRunsPolicy ());// saturation strategy, simply put, it is lined up behind the threads were waiting. 34 // rejected task to run in the main thread, the main thread is blocked, other tasks will only continue to be committed to the implementation of the thread pool after being rejected executing the task 35 36 for ( int i = 0; I <. 5; I ++ ) { 37 [ // Create Object WorkerThread (WorkerThread class implements Runnable interface) is a Runable each task 38 is // Runnable MyRunnable new new worker = ( "" + I); 39 ticketRunnable ticketRunnable = new new ticketRunnable (); 40 // execute the Runnable 41 is Executor.execute (ticketRunnable); 42 is 43 is } 44 is // terminate thread pool 45 //void shutdown () Initiates an orderly shutdown, perform tasks previously submitted, but does not accept new tasks. If you have already closed, the call has no other effect. 46 is executor.shutdown (); 47 // Boolean isTerminated () 48 // If after closing all the tasks have completed, it returns true. Note that unless you first call shutdown or shutdownNow, otherwise isTerminated never be true. 49 the while (! {Executor.isTerminated ()) 50 // System.out.println ( "thread pool is not completely closed yet !!!"); 51 is } 52 is System.out.println ( "Finished All Threads" ); 53 is } 54 }