Week1 -Threading Flashcards
Multi threading
two or more part that can run concurrently. They can run at the same time, while making optimal use of available resources, such as the CPU.
Thread Life Cycle
The life cycle of a variety of stages: Born, Started, Run, Die.
Thread States
The thread states are
- New
- Runnable
- Block
- Wait
- Timed_Waiting
- Terminate
New
Is the born thread state. It stays in this state until the program starts the thread.
Runnable
The thread has been started and is executing a task.
Block
when a thread try’s to acquire an object lock, but said lock is being used by another thread. It will remain in this state until the other thread has finished an releases the lock
Wait
a thread goes to wait state once its calls the wait() method on an object. It will need to wait indefinitely for another thread to perform a particular task.
Timed_Waiting
thread is waiting for another thread to perform a particular action for a specific amount of time. The thread transitions back to Runnable after interval time has expired or when the event its waiting for occurs.
Terminate
thread completes execution. terminated.
Thread Priorities
helps the operation system to determine the order in which threads are scheduled. Thread priority does not guarantee the order in which the threads will execute. and are very platform dependent.
Thread Priority range
MIN_PRIORITY - CONSTANT 1
MAX_PRIORITY CONSTANT 10.
By default every thread is given a priority of NORM_PRIORITY CONSTANT 5.
Joining Threads
Allow one thread to wait for another thread to finish its execution. If t is a thread object whose threads are currently executing. Then t.join() will make sure that the t is terminated, before next instructions are executed by the program. If there are multiple threads calling join() method, it mean overloading on the joins allows the programmer to specify a waiting period.
3 overload functions
join()
join(long millis)
join( long millis, int nanos)
join()
waits for the thread to die. It will put the current thread on wait until the thread in which it was called is dead. If interrupted will throw an interrupted exception.
syntax: public final void join(){
do something
}
join(long millis)
waits at the most millis millisecond for the thread to die.
syntax: public final synchronized void join (long millis){
do something
}