이 글은 주로 Java ThreadPoolExecutor의 매개변수에 대한 심층적인 이해를 위한 관련 정보를 소개하고 있습니다. 필요한 친구는
Java ThreadPoolExecutor의 매개변수에 대한 심층적인 이해를 참고하세요.
1. 실행자를 사용하여 스레드 풀을 만듭니다. 물론 Executor는 new ThreadPoolExecutor
에 대해 다른 매개 변수를 사용합니다. 1. newFixedThreadPool()
고정된 수의 스레드로 스레드 풀을 만듭니다. LinkedBlockingQueue를 사용하므로 maximumPoolSize는 쓸모가 없습니다. corePoolSize가 가득 차면 LinkedBlockingQueuequeue에 추가됩니다. 스레드 실행이 완료될 때마다 LinkedBlockingQueue 대기열에서 하나를 가져옵니다. 따라서 고정된 크기의 스레드 풀이 생성됩니다.
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }
생성자
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }
작업 실행 순서:
1. 스레드 수가 corePoolSize보다 적으면 스레드를 생성하여 작업을 실행합니다. 2. 스레드 개수가 corePoolSize보다 크거나 같고 workQueue가 가득 차지 않은 경우 workQueue에 넣습니다.3. 스레드 개수가 다음보다 크거나 같습니다. corePoolSize 및 workQueue가 가득 차면 새 작업이 생성되고 스레드가 실행됩니다. 총 개수는 maximumPoolSize
보다 작아야 합니다. 4. 총 스레드 개수가 maximumPoolSize 및 workQueue와 같을 때. 꽉 차면 핸들러의 거부된Execution이 실행됩니다. 그것이 바로 거절 전략이다. <… 2. ThreadPoolExecutor.CallerRunsPolicy() 실행 메소드를 직접 호출하고 실행을 차단 3. ThreadPoolExecutor.DiscardPolicy() 후속 작업을 직접 삭제 4. ThreadPoolExecutor.DiscardOldestPolicy() 대기열 리더 삭제 물론 작업은RejectedExecutionHandler를 상속하여 거부 정책을 작성할 수 있습니다.
int corePoolSize = 1; int maximumPoolSize = 2; int keepAliveTime = 10; // BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(); BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(5); ThreadFactory threadFactory = Executors.defaultThreadFactory(); //线程池和队列满了之后的处理方式 //1.跑出异常 RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy(); RejectedExecutionHandler handler2 = new ThreadPoolExecutor.CallerRunsPolicy(); RejectedExecutionHandler handler3 = new ThreadPoolExecutor.DiscardPolicy(); RejectedExecutionHandler handler4 = new ThreadPoolExecutor.DiscardOldestPolicy(); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, handler2); for (int j = 1; j < 15; j++) { threadPoolExecutor.execute(new Runnable() { public void run() { try { System.out.println(Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } }); } System.out.println(threadPoolExecutor); }
위 내용은 Java ThreadPoolExecutor 매개변수를 심층적으로 이해하기 위한 샘플 코드 분석의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!