|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.thebuzzmedia.imgscalr.AsyncScalr
public class AsyncScalr
Class used to provide the asynchronous versions of all the methods defined in
Scalr
for the purpose of offering more control over the scaling and
ordering of a large number of scale operations.
Scalr
class in new Callable
instances that are submitted to an internal
ExecutorService
for execution at a later date. A Future
is
returned to the caller representing the task that will perform the scale
operation. Future.get()
or Future.get(long, TimeUnit)
can be
used to block on the returned Future
, waiting for the scale
operation to complete and return the resultant BufferedImage
.
This design provides the following features:
get()
or get(long, TimeUnit)
immediately on the
returned Future
from any of the methods below.ExecutorService
for processing scale operations for maximum
flexibility; otherwise this class utilizes a fixed ThreadPoolExecutor
via Executors.newFixedThreadPool(int)
that will create the given
number of threads and let them sit idle, waiting for work.
BufferedImage
and stored in the JVM Heap space (decoded image
instances are always larger than the source images on-disk). For larger
images, that can use up quite a bit of memory. You will need to benchmark
your particular use-cases on your hardware to get an idea of where the sweet
spot is for this; if you are operating within tight memory bounds, you may
want to limit simultaneous scaling operations to 1 or 2 regardless of the
number of cores just to avoid having too many BufferedImage
instances
in JVM Heap space at the same time.
These are rough metrics and behaviors to give you an idea of how best to tune
this class for your deployment, but nothing can replacement writing a small
Java class that scales a handful of images in a number of different ways and
testing that directly on your deployment hardware. *
ExecutorService
utilized by this class won't be initialized until
the class is referenced for the first time or explicitly set with one of the
setter methods. More specifically, if you have no need for asynchronous image
processing offered by this class, you don't need to worry about wasted
resources or hanging/idle threads as they will never be created if you never
reference this class.
Field Summary | |
---|---|
static int |
DEFAULT_THREAD_COUNT
Default thread count used to initialize the internal ExecutorService if a count isn't specified via
setServiceThreadCount(int) before this class is used. |
Constructor Summary | |
---|---|
AsyncScalr()
|
Method Summary | |
---|---|
static ExecutorService |
getService()
Used to get access to the internal ExecutorService used by this
class to process scale operations. |
static Future<BufferedImage> |
resize(BufferedImage src,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
Scalr.Rotation rotation,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
Scalr.Rotation rotation,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Rotation rotation,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Rotation rotation,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Mode resizeMode,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Mode resizeMode,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Mode resizeMode,
Scalr.Rotation rotation,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Mode resizeMode,
Scalr.Rotation rotation,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Rotation rotation,
int targetSize,
BufferedImageOp... ops)
|
static Future<BufferedImage> |
resize(BufferedImage src,
Scalr.Rotation rotation,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
|
static void |
setService(ExecutorService service)
Used to initialize the internal ExecutorService which runs tasks
generated by this class with the given service. |
static void |
setServiceThreadCount(int threadCount)
Used to adjust the fixed number of threads (min/max) used by the internal ThreadPoolExecutor to executor scale operations. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int DEFAULT_THREAD_COUNT
ExecutorService
if a count isn't specified via
setServiceThreadCount(int)
before this class is used.
Default value is 2
.
Constructor Detail |
---|
public AsyncScalr()
Method Detail |
---|
public static ExecutorService getService()
ExecutorService
used by this
class to process scale operations.
NOTE: You will need to explicitly shutdown any service
currently set on this class before the host JVM exits unless you
have passed in a custom ExecutorService
that specifically
creates/uses daemon threads (which will exit immediately).
You can call ExecutorService.shutdown()
to wait for all scaling
operations to complete first or call
ExecutorService.shutdownNow()
to kill any in-process operations
and purge all pending operations before exiting.
ExecutorService
used by this class to process
scale operations.public static void setService(ExecutorService service) throws IllegalArgumentException
ExecutorService
which runs tasks
generated by this class with the given service.
NOTE: This operation will call
ExecutorService.shutdown()
on any existing
ExecutorService
currently set on this class. This means this
operation will block until all pending (queued) scale operations are
completed.
service
- A specific ExecutorService
instance that will be used
by this class to process scale operations.
IllegalArgumentException
- if service
is null
.public static void setServiceThreadCount(int threadCount) throws IllegalArgumentException
ThreadPoolExecutor
to executor scale operations.
The following logic is used when applying thread count changes using this
method:
ThreadPoolExecutor
is created with the given fixed number of
threads.ThreadPoolExecutor
then the methods
ThreadPoolExecutor.setCorePoolSize(int)
and
ThreadPoolExecutor.setMaximumPoolSize(int)
are used to adjust the
current fixed size of the thread pool without destroying the executor and
creating a new one. This avoids unnecessary garbage for the GC and helps
keep the task queue intact.ThreadPoolExecutor
, then it will be shutdown after all pending
tasks have completed and replaced with a new instance of type
ThreadPoolExecutor
with the given number of fixed threads.ThreadPoolExecutor
thread count is
adjusted, if the given threadCount
is smaller than the
current number of threads in the pool, the extra threads will only be
killed after they have completed their work and become idle. No scaling
operations will be interrupted.
threadCount
- The fixed number of threads (min/max) that the service will be
configured to use to process scale operations.
IllegalArgumentException
- if threadCount
is < 1.public static Future<BufferedImage> resize(BufferedImage src, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Rotation rotation, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Method scalingMethod, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Rotation rotation, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Mode resizeMode, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Mode resizeMode, Scalr.Rotation rotation, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Mode resizeMode, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Mode resizeMode, Scalr.Rotation rotation, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Rotation rotation, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Method scalingMethod, int targetWidth, int targetHeight, BufferedImageOp... ops)
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Rotation rotation, int targetWidth, int targetHeight, BufferedImageOp... ops)
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Mode resizeMode, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Mode resizeMode, Scalr.Rotation rotation, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Mode resizeMode, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
public static Future<BufferedImage> resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Mode resizeMode, Scalr.Rotation rotation, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
IllegalArgumentException
|
Copyright 2011 The Buzz Media, LLC | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |