aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java
blob: fe6ee2035ccfc0c861286c5de7287640fa46b127 (plain)
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
package me.lucko.spark.profiler;

import com.google.common.base.Preconditions;

import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Builds {@link Sampler} instances.
 */
public class SamplerBuilder {

    private int samplingInterval = 10;
    private long timeout = -1;
    private ThreadDumper threadDumper = new ThreadDumper.All();

    public SamplerBuilder() {
    }

    public SamplerBuilder samplingInterval(int samplingInterval) {
        this.samplingInterval = samplingInterval;
        return this;
    }

    public SamplerBuilder completeAfter(long timeout, TimeUnit unit) {
        Preconditions.checkArgument(timeout > 0, "time > 0");
        this.timeout = System.currentTimeMillis() + unit.toMillis(timeout);
        return this;
    }

    public SamplerBuilder threadDumper(ThreadDumper threadDumper) {
        this.threadDumper = threadDumper;
        return this;
    }

    public Sampler start(Timer samplingThread, ExecutorService workerPool) {
        Sampler sampler = new Sampler(this.samplingInterval, this.threadDumper, this.timeout);
        sampler.start(samplingThread, workerPool);
        return sampler;
    }

}