OCP 17 Exam - chapter 13 notes - concurrency
Executors
be aware whether
Executors.newSingleThreadExecutor
is used, because running on single thread automatically synchronizes executions, so result will be always consistent (Q.22)awaitTermination(1, TimeUnit.SECONDS)
returnsfalse
instead of throwing timeout when tasks execution didn’t finish (Q.12)- be aware of methods of
ScheduledExecutorService
- only 1 acceptsCallable
(Q.4):ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
scheduleAtFixedRate
will always wait for task to complete if overdue, but then starts new one immediately. Thus fixed rate is not really strict that task must be executed at given time.- be aware of difference between
execute
andsubmit
. First returnsvoid
, second returnsFuture
. (Q.20)
Reentrant Lock
we could
lock
many times w/o exception (holdCount
will increase), but callingunlock
when not locked, will throwIllegalMonitorStateException
also because
lock
could be called multiple times, theunlock
should be the same number of times aslock
to release lock (Q.17)tryLock(long timeout, TimeUnit unit)
works that waits specified amount of time for lock, but returnsfalse
instead throwing timeout exception
Runnable vs Callable
Callable
could throw check Exception, butRunnable
does not (Q.3)- quote from javadoc of
Callable
:A Runnable, however, does not return a result and cannot throw a checked exception.
- quote from javadoc of
Threads
be aware of
Thread#run
vsThread#start
. First “runs” always in the invoking thread, while “start” starts new thread so result would be unpredictable. (Q.8)Thread#interrupt
might be ignored by running tread or even lock.InterruptedException
is checked exception, so must be explicitly declared when callingThread#sleep
orjava.util.concurrent.locks.ReentrantLock.lockInterruptibly
. (Q.16)- race condition is when two threads wants to modify the same resource at the same time and result would be invalid
- live lock is when to threads are blocked forever but appear to be active
Parallel stream
findFirst
will always return first element, no matter parallel stream is used (Q.21)
Playground code
https://github.com/RG9/rg-playground-ocp17/tree/main/Chapter13/src/test/java/pl/rg9/demo
Credit: OCP Oracle Certified Professional Java SE 17 Developer Study Guide