Let's experiment with tasks and tread pools.
Write a class to implement a single task both as a
Runnable
and as a Callable<String>
.
The task has just to extract a random number, which must be
eventually printed out.
The driver program has to create a set of tasks, and
execute/submit them using an Executor
.
Possibly, make the program configurable so to decide about
using Executable
vs Callable
, and
about different types of Executor
s.
Let's experiment a tread starvation deadlock,
with bounded-size thread pool.
A shared int
variable c
is
initialized to INITVALUE
;
each task, has to: i) decrement the variable; ii) wait as
long as the value of c
is positive; iii)
subsequently, it must notify one waiting thread.
Deadlock shows up whenever c
has no chance to reach
negative values.
A similar behavior can be obtained using
a CountDownLatch
; try to implement also this
solution.
Rework the previous exercise to make the program get to livelock (instead of deadlock).
Try to implement a parallelized version of the famous algorithm known as "Sieve of Eratosthenes," which finds all the prime numbers in the interval [2,N], given the integer N.