Package com.gengoai.io
Class ResourceMonitor
- java.lang.Object
-
- java.lang.Thread
-
- com.gengoai.io.ResourceMonitor
-
- All Implemented Interfaces:
Runnable
public class ResourceMonitor extends Thread
A common pitfall in Java is not properly closing resources. This can become especially tricky when dealing with concurrency and the new Java stream framework. Mango provides a `ResourceMonitor` which tracks `MonitoredObjects` and automatically closes (frees) them when they are no longer referenced. The `ResourceMonitor` is basically a garbage collector for resources!
- Author:
- David B. Bracewell
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
-
-
Field Summary
Fields Modifier and Type Field Description protected static ResourceMonitor
MONITOR
-
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> T
addResource(@NonNull Object referent, T resource)
protected <T> T
addResource(Object referent, T resource, Consumer<T> onClose)
static <T> MStream<T>
monitor(@NonNull MStream<T> stream)
Monitors the given MStreamstatic InputStream
monitor(@NonNull InputStream stream)
Monitors the given InputStream.static OutputStream
monitor(@NonNull OutputStream stream)
Monitors the given OutputStream.static Reader
monitor(@NonNull Reader reader)
Monitors the given Reader.static Writer
monitor(@NonNull Writer writer)
Monitors the given Writer.static Connection
monitor(@NonNull Connection connection)
Monitors the given SQL Connection closing it when it is no longer referenced.static DoubleStream
monitor(@NonNull DoubleStream stream)
Monitors the given DoubleStreamstatic IntStream
monitor(@NonNull IntStream stream)
Monitors the given IntStreamstatic LongStream
monitor(@NonNull LongStream stream)
Monitors the given LongStreamstatic <T> Stream<T>
monitor(@NonNull Stream<T> stream)
Monitors the given Stream.static <T> MonitoredObject<T>
monitor(T object)
Monitors the given Objectstatic <T> MonitoredObject<T>
monitor(T object, @NonNull Consumer<T> onClose)
Monitors the given Object using the given onClose commandvoid
run()
-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
-
-
-
Field Detail
-
MONITOR
protected static final ResourceMonitor MONITOR
-
-
Method Detail
-
monitor
public static Connection monitor(@NonNull @NonNull Connection connection)
Monitors the given SQL Connection closing it when it is no longer referenced. Note that connection is only closed and not committed.- Parameters:
connection
- the connection- Returns:
- the connection
-
monitor
public static InputStream monitor(@NonNull @NonNull InputStream stream)
Monitors the given InputStream.- Parameters:
stream
- the stream- Returns:
- the input stream
-
monitor
public static OutputStream monitor(@NonNull @NonNull OutputStream stream)
Monitors the given OutputStream.- Parameters:
stream
- the stream- Returns:
- the output stream
-
monitor
public static Reader monitor(@NonNull @NonNull Reader reader)
Monitors the given Reader.- Parameters:
reader
- the reader- Returns:
- the reader
-
monitor
public static Writer monitor(@NonNull @NonNull Writer writer)
Monitors the given Writer.- Parameters:
writer
- the writer- Returns:
- the writer
-
monitor
public static <T> Stream<T> monitor(@NonNull @NonNull Stream<T> stream)
Monitors the given Stream.- Type Parameters:
T
- the type parameter- Parameters:
stream
- the stream- Returns:
- the stream
-
monitor
public static <T> MStream<T> monitor(@NonNull @NonNull MStream<T> stream)
Monitors the given MStream- Type Parameters:
T
- the type parameter- Parameters:
stream
- the stream- Returns:
- the m stream
-
monitor
public static DoubleStream monitor(@NonNull @NonNull DoubleStream stream)
Monitors the given DoubleStream- Parameters:
stream
- the stream- Returns:
- the double stream
-
monitor
public static IntStream monitor(@NonNull @NonNull IntStream stream)
Monitors the given IntStream- Parameters:
stream
- the stream- Returns:
- the int stream
-
monitor
public static LongStream monitor(@NonNull @NonNull LongStream stream)
Monitors the given LongStream- Parameters:
stream
- the stream- Returns:
- the long stream
-
monitor
public static <T> MonitoredObject<T> monitor(@NonNull T object)
Monitors the given Object- Type Parameters:
T
- the type parameter- Parameters:
object
- the object- Returns:
- the monitored object
-
monitor
public static <T> MonitoredObject<T> monitor(@NonNull T object, @NonNull @NonNull Consumer<T> onClose)
Monitors the given Object using the given onClose command- Type Parameters:
T
- the type parameter- Parameters:
object
- the objectonClose
- the on close- Returns:
- the monitored object
-
addResource
protected <T> T addResource(@NonNull @NonNull Object referent, @NonNull T resource)
-
-