public class ExecuteWorker extends Object
ExecutorService
to manage the task, I create 3 type of execution Schedule
- to schedule execute action with delay until some imeexecute(Callable)
- To do some task in the backgroundexecuteAndWaitAll(Runnable...)
or executeAndWaitAny(Callable[])
- Exxecute some very task and want program to wait until the task was endshutdown()
or forceShutdown()
to stop and terminal the thread
Example:
// create new thread
ExecuteWorker thread = new ExecuteWorker(Executors.newScheduledThreadPool(1));
// print "Hello world!" in every 1 second for 1 minutes
thread.schedule(() -> System.out.println("Hello world!"), 0, 1, 60, TimeUnit.SECONDS);
// close the thread
multiThread.shutdown();
ExecutorService
,
Executors
,
Future
Modifier and Type | Class and Description |
---|---|
class |
ExecuteWorker.ScheduleFutureImp
This class implemented because I need the easy way to manage the
ScheduledFuture This class have new method call ExecuteWorker.ScheduleFutureImp.waitAndDone() so that program will wait until program done |
Constructor and Description |
---|
ExecuteWorker(ExecutorService service)
create multi-thread service.
|
Modifier and Type | Method and Description |
---|---|
static void |
doBackground(Runnable run)
do
run on background using SwingWorker |
static void |
doBackground(Runnable run,
Runnable done)
do
run on background using SwingWorker |
<T> Future<T> |
execute(Callable<T> callable)
To execute method
Runnable.run() in the background |
Future<?> |
execute(Runnable runnable)
To execute method
Runnable.run() in the background |
<T> List<Future<T>> |
executeAndWaitAll(Callable<T>... callable)
execute the tasks and wait until all task was successfully
|
List<Future<?>> |
executeAndWaitAll(Runnable... runnable)
convert to
Callable and run executeAndWaitAll(Callable[]) |
<T> T |
executeAndWaitAny(Callable<T>... callable)
execute the tasks and wait until some of task was successfully (require only one task and method will done)
|
void |
executeAndWaitAny(Runnable... runnable)
convert to
Callable and run executeAndWaitAny(Callable[]) |
List<Runnable> |
forceShutdown()
force shutdown and return the task that not execute successfully.
|
boolean |
isShutDown()
to check is this class already shutdown
|
boolean |
isTerminate()
Inherit docs from
ExecutorService.isTerminated() Returns true if all tasks have completed following shut down. |
ExecuteWorker.ScheduleFutureImp |
schedule(Runnable runnable,
int initial,
int delay,
int until,
TimeUnit unit,
boolean fix)
The service must be
ScheduledExecutorService class the scheduling the task in time interval at the future, |
void |
shutdown()
if create this class, you must shutdown it at last
|
static <T> Callable<T> |
toCall(Runnable runnable,
T returnValue)
convert runnable to callable with return value is parameter
returnValue |
static Runnable |
toRun(Callable<?> callable)
convert callable to runnable with ignore all exception and return value.
|
boolean |
wait(long time,
TimeUnit unit)
Blocks until all tasks have completed.
|
public ExecuteWorker(ExecutorService service)
Executors
factoryservice
- the running servicepublic ExecuteWorker.ScheduleFutureImp schedule(Runnable runnable, int initial, int delay, int until, TimeUnit unit, boolean fix)
ScheduledExecutorService
class
And because of ScheduledExecutorService don't have method that can rerun the task and end at some time so I need to implement by myself by using Future.cancel(boolean)
method to cancel it when task should be end.
Example:
runnable
- method that want to executeinitial
- the time interval at first run runnable
delay
- the time intervaluntil
- the time endunit
- unit if the timefix
- true
when the delay
time meaning delay time will fixed (not whether execute time is what but program will add more delay time on it); otherwise is false
true
you can learn more at ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
false
you can learn more at ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
ScheduledFuture
- this class will let you know what going on, since this method will close from cancellation only so you cannot use the method get()
normallypublic Future<?> execute(Runnable runnable) throws RejectedExecutionException
Runnable.run()
in the backgroundrunnable
- run taskFuture.get()
when task execute successfully, it's wil return null
RejectedExecutionException
- if the task cannot be scheduled for executionNullPointerException
- if runnable is nullFuture
public <T> Future<T> execute(Callable<T> callable) throws RejectedExecutionException
Runnable.run()
in the backgroundT
- the return object valuecallable
- the run taskFuture.get()
when task execute successfully, it's wil return object
T
RejectedExecutionException
- if the task cannot be scheduled for executionNullPointerException
- if runnable is nullFuture
@SafeVarargs public final <T> List<Future<T>> executeAndWaitAll(Callable<T>... callable) throws RejectedExecutionException
T
- the return typecallable
- the run taskNull
if there have InterruptedException
causeRejectedExecutionException
- if the task cannot be scheduled for executionNullPointerException
- if runnable is nullFuture
@SafeVarargs public final <T> T executeAndWaitAny(Callable<T>... callable) throws RejectedExecutionException
T
- the return typecallable
- the run taskNull
if there have InterruptedException
causeRejectedExecutionException
- if the task cannot be scheduled for executionNullPointerException
- if runnable is nullFuture
public List<Future<?>> executeAndWaitAll(Runnable... runnable) throws RejectedExecutionException
Callable
and run executeAndWaitAll(Callable[])
runnable
- the run taskFuture.get()
will return null if successful), or Null
if there have InterruptedException
causeRejectedExecutionException
- if the task cannot be scheduled for executionNullPointerException
- if runnable is nullFuture
public void executeAndWaitAny(Runnable... runnable) throws RejectedExecutionException
Callable
and run executeAndWaitAny(Callable[])
runnable
- the run taskRejectedExecutionException
- if the task cannot be scheduled for executionNullPointerException
- if runnable is nullFuture
public void shutdown()
public boolean isShutDown()
true
, if already shutdown; otherwise, return false
ExecutorService.shutdown()
public boolean isTerminate()
ExecutorService.isTerminated()
true
if all tasks have completed following shut down.
Note that isTerminated
is never true
unless
either shutdown
or shutdownNow
was called first.true
, if already terminal; otherwise, return false
ExecutorService.isTerminated()
public List<Runnable> forceShutdown()
ExecutorService.shutdownNow()
public boolean wait(long time, TimeUnit unit) throws InterruptedException
shutdown()
or forceShutdown()
~ result will be Boolean.TRUE
Boolean.FALSE
InterruptedException
time
- the maximum time for waitingunit
- the unit for waiting timeInterruptedException
- when error occurred while waitingExecutorService.awaitTermination(long, TimeUnit)
public static void doBackground(Runnable run)
run
on background using SwingWorker
run
- runner codepublic static void doBackground(Runnable run, Runnable done)
run
on background using SwingWorker
run
- runner codedone
- when run complete, it's will run this parameterpublic static Runnable toRun(Callable<?> callable)
callable
- call that want to conversionRunnable
Copyright © 2017. All rights reserved.