Concurrency 2 : Thread Management Flashcards
What are the 3 main threading issues encountered?
Deadlock, Livelock, Starvation
Define Deadlock…
When 2 or more threads are left waiting infinitely to proceed because they need resources that the other is using. This is known as the circular wait. E.g Thread A is waiting for Thread B and vice versa, thus neither can resume.
Define Livelock…
When 2 or more threads are stuck in a loop of transferring resources to one another. Thus, preventing them carrying out other tasks.
Define Starvation…
When one or more threads are waiting for a resource, but another thread is using the resource in an excessive way, thus starving the other threads of the resource.
On what class levels can thread management occur?
Object level and Thread level.
What are the 3 main object level thread management methods? Define each…
Must be in a synchronised block and do not need an invoking object.
wait() - Unlocks the objects monitor to allow other threads to enter the object.
notify() - Notifies the next waiting thread to resume execution.
notifyAll() - Notifies all waiting threads to resume execution.
What are the 3 main thread based thread management methods? Define each…
All are static methods, and can be invoked anywhere.
Thread.sleep() - Causes the current thread to pause for a specified amount of time.
Thread.join() - Makes the invoking thread wait for the completion of another thread.
Thread.yield() - Allows the thread to yield its usage of the processor so the processor can operate other threads of a higher priority.
What are the differences between wait() and Thread.sleep()?
- wait() is object level and doesn’t need an invoking object
- Thread.sleep() must be invoked from a synchronised block.
- wait() is woken up by a notify call where as sleep() is woken by an interruption or time exceeded.
Explain the Thread lifecycle. Draw a diagram…
New - Thread is created.
Runnable - Thread is waiting for the OS to schedule its running.
Running - Thread is executing via the run() method.
Waiting/Sleeping/Blocking - Thread is waiting or sleeping until notified, in which case it goes back to the runnable stage.
Dead - Thread terminates.
Define Explicit Locking… How is it different from automatic locking…
Explicit locking enables us to create a lock object, and manually lock and unlock objects as we wish. This differentiates from synchronised since sync locks are invisible and automated.
Can we use explicit locking on more than one object?
Yes, we can create a lock and use it across multiple objects.