Concurrency II Flashcards
What does join() do
Allows one thread to wait for the completion of another
Can respond to an interrupt
What does yield() do?
Pauses currently executing thread and allows other threads of other priorities to execute
Name the 5 states in the lifecycle of a thread
New => Initializing the thread
Runnable => start() must be called to get to this state, runnable is when the thread is ready to be run by waiting for CPU time to run
Running => Currently being processed by CPU. Changing from runnable to running state is OS dependent, since this is what schedules thread timeslices.
Dead => thread execution complete
Waiting/ Sleeping/ Blocking => when the thread in running state. One of the methods stated are called, must wait to be notified/ sleep time to be finished before going back to runnable state.
Give 3 differences between wait and sleep
Wait: must be in a block that is synchronized to the monitor object
Sleep: doesn’t need to occur in a synchronized block
Wait: releases the lock when called (doesn’t sacrifice remainder timeslice)
Sleep: Retains the lock and sacrifices the remainder of its timeslice
Wait: Wakes up when notified
Sleep: Woken up by an interruption
What do the methods wait() and notify() do
Objects are monitored using a lock; wait() releases the lock, notify() hands the lock to the waiting thread once the block that notified the thread completes execution
wait() => waits for a condition to occur, must call from synchronized methods
notify() => notifies a waiting thread that a condition has occurred
What is livelock
When two threads are too busy responding to each other to do work
1) What is starvation
When does it occur most often
1) When a thread can’t gain access to shared resources because another thread is frequently calling the objects’ method.
2) When a thread is synchronized with methods that has faster execution times
What is a deadlock
Occurs when two or more processes are waiting for each other to release a resource
Give 3 differences between wait and sleep
Wait: must be in a block that is synchronized to the monitor object
Sleep: doesn’t need to occur in a synchronized block
Wait: releases the lock when called (doesn’t sacrifice remainder timeslice)
Sleep: Retains the lock and sacrifices the remainder of its timeslice
Wait: Wakes up when notified
Sleep: Woken up by an interruption