001package org.kuali.common.jute.runtime; 002 003import static org.kuali.common.jute.base.Precondition.checkMin; 004 005import java.lang.management.ManagementFactory; 006import java.lang.management.ThreadMXBean; 007 008import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 009 010@JsonDeserialize(builder = Threads.Builder.class) 011public final class Threads { 012 013 private final int current; 014 private final int daemon; 015 private final int peak; 016 017 private Threads(Builder builder) { 018 this.current = builder.current; 019 this.daemon = builder.daemon; 020 this.peak = builder.peak; 021 } 022 023 public static Threads build() { 024 ThreadMXBean bean = ManagementFactory.getThreadMXBean(); 025 Builder builder = builder(); 026 builder.withCurrent(bean.getThreadCount()); 027 builder.withDaemon(bean.getDaemonThreadCount()); 028 builder.withPeak(bean.getPeakThreadCount()); 029 return builder.build(); 030 } 031 032 public static Builder builder() { 033 return new Builder(); 034 } 035 036 public static class Builder implements org.apache.commons.lang3.builder.Builder<Threads> { 037 038 private int current; 039 private int daemon; 040 private int peak; 041 042 public Builder withCurrent(int current) { 043 this.current = current; 044 return this; 045 } 046 047 public Builder withDaemon(int daemon) { 048 this.daemon = daemon; 049 return this; 050 } 051 052 public Builder withPeak(int peak) { 053 this.peak = peak; 054 return this; 055 } 056 057 @Override 058 public Threads build() { 059 return validate(new Threads(this)); 060 } 061 062 private static Threads validate(Threads instance) { 063 checkMin(instance.current, 0, "current"); 064 checkMin(instance.daemon, 0, "daemon"); 065 checkMin(instance.peak, 0, "peak"); 066 return instance; 067 } 068 } 069 070 public int getCurrent() { 071 return current; 072 } 073 074 public int getDaemon() { 075 return daemon; 076 } 077 078 public int getPeak() { 079 return peak; 080 } 081 082}