summaryrefslogtreecommitdiffstats
path: root/docproc/src/main/java/com/yahoo/docproc/jdisc/DocprocThreadPoolExecutor.java
blob: 8e4fb1d471faa02747bb2d9e47182ab13df98ed6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docproc.jdisc;

import com.yahoo.concurrent.DaemonThreadFactory;
import com.yahoo.log.LogLevel;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/**
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public class DocprocThreadPoolExecutor extends ThreadPoolExecutor {

    private static Logger log = Logger.getLogger(DocprocThreadPoolExecutor.class.getName());
    private DocprocThreadManager threadManager;

    public DocprocThreadPoolExecutor(int maxNumThreads, BlockingQueue<Runnable> queue, DocprocThreadManager threadMgr) {
        super((maxNumThreads > 0) ? maxNumThreads : Runtime.getRuntime().availableProcessors(),
              (maxNumThreads > 0) ? maxNumThreads : Runtime.getRuntime().availableProcessors(),
              5, TimeUnit.MINUTES,
              queue,
              new DaemonThreadFactory("docproc-"));
        this.threadManager = threadMgr;
        allowCoreThreadTimeOut(false);
        log.log(LogLevel.DEBUG, "Created docproc thread pool with " + super.getCorePoolSize() + " worker threads.");
    }

    @Override
    protected void beforeExecute(Thread thread, Runnable runnable) {
        threadManager.beforeExecute((DocumentProcessingTask) runnable);
    }

    @Override
    protected void afterExecute(Runnable runnable, Throwable throwable) {
        threadManager.afterExecute((DocumentProcessingTask) runnable);
    }

    @Override
    public void shutdown() {
        super.shutdown();
        threadManager.shutdown();
    }

    @Override
    public List<Runnable> shutdownNow() {
        List<Runnable> list =  super.shutdownNow();
        threadManager.shutdown();
        return list;
    }

    boolean isAboveLimit() {
        return threadManager.isAboveLimit();
    }
}