Java has `Callable` interface, that
belongs to `java.util.concurrent`
package, which has a method `call()`.
In order to
understand this context, its quite simple that concurrent package has
`Callable`
interface, this `call()`
method could return object at run-time.
`FutureTask`
can take Callable Object through its
constructor. For the `FutureTask`
there is a
`run()`
method, served by Inheritance Hierarchy
by `RunnableFuture`.
So
when we extend `FutureTask`
in order create Executor Framework task,
its our responsibility to override `run()`
method within the class which we create (extending `FutureTask`).
Once we do that
we get the body which act as execution block of Executor. Afterwards
this `FurureTask`
object can be passed to the framework based on the need by `V
call()` method in run-time.
This can be done by writing Governance
classes which extends `Callable`
Interface.
At run-time,
`FutureTask run()`
body is monitored & controlled by
these methods
cancel(boolean);
isCancelled();
isDone();
V get()
throws InterruptedException, ExecutionException;
V get(long,
java.util.concurrent.TimeUnit) throws InterruptedException,
ExecutionException, TimeoutException;
boolean
isCancelled();
boolean
isDone();
boolean
cancel(boolean);
protected
void done();
protected
void set(V);
protected
void setException(java.lang.Throwable);
protected
boolean runAndReset();
`run()`
method can be started by threading mechanism,- `thread_obj.start()`,
rather we can go for `Executor` framework `execute()`
which will take care of running FutureTask `run()`
method. Over this we can have Fork, Enqueue,
Dequeue, can do Atomic operations, Theadpool, Concurrency. And
we could have `ReentrantLock`,
`StampedLock`, `ReadWriteLock`,
`ReentrantReadWriteLock`
`ExecutorService`
class can make goverance to overall tasks, future tasks spawn.
It serves method to shutdown the processes. We could judge the
processes been shutdown or terminated. `shutdownNow()`
shuts all the executor, future services and gives thead objects list.
And we could presume by `invokeAll(? extends
Callable )`,
`invokeAny(? extends Callable)`
also passing `java.util.concurrent.TimeUnit`
is possible.
And by using
`submit(Thread or Callable)`, we could
instantiate Future object, it accepts either Thread or Callable.
For recursion,
we can make use of these classes
`RecursiveAction`
`RecursiveTask`
We could delay
the process by class by extending & overriding following
interface
`Delayed.getDelay(java.util.concurrent.TimeUnit)`
In these
classes delay of `java.util.concurrent.TimeUnit`
is been taken care explicitly
`ScheduledExecutorService`
`ScheduledFuture`
`java.io.Closable`
is abided within these FutureTask operations implicitly.
Following
exception may arise while we work with `FutureTask`, `Executors`
`CompletionException`
`BrokenBarrierException`
`CancellationException`
`ExecutionException`
`RejectedExecutionException`
`TimeoutException`
No comments:
Post a Comment