Threads Flashcards
Process
Programs can have many threads
Thread
Path of execution within a process
Every thread has its own _____, but shares memory with other threads
Call Stacks
isAlive()
tests if this thread is alive
join()
waits for the thread to die
join(long millis)
waits at most millis milliseconds for the thread to die
join(long millis, int nanos)
waits at most millis milliseconds plus nanos nanoseconds for the thread to die
3 threads created at start of Java Program
Main
Garbage Collector
Thread Scheduler
Garbage Collector
Daemon Thread
Intermittently removes dereferenced objects from memory
Can not force
System.gc() requests garbage collector
Garbage Collector: finalize()
Used to intervene when garbage collection occurs
Can Threads be garbage collected?
Yes, but only if it is not active
Synchronized
keyword that ensures that only one thread can access a resource at a given time
By synchronizing, you gain ____ but lose ___
Consistency, Speed
Thread Safe
one thread at a time can access
Thread Methods
start() calls run()
getPriority() values from 1 to 10
setPriority() values from 1 to 10
isAlive() checks whether the thread is running
wait() specify the amount of time to wait(can be notified)
sleep() will pause for specified amount of time(cannot be notified)
notifiy() wake up waiting threads
notifiyAll() wake up all waiting threads
join() wait for another thread to stop executing, catches up with another thread
Thread State: New
Has not started yet
Thread State: Runnable
currently executing
Thread State: Blocked
waiting for a lock to be released
Thread State: Waiting
Thread is waiting indefinitely for another thread to perform a particular action
wait()/join() with no timeout
Thread State: Timed_Waiting
Thread is waiting for a another thread to perform a particular action for a specified waiting time
sleep(), wait()/join() with timeout
Thread State: Terminated
Thread completed execution, terminated
Deadlocked
When multiple threads are blocking each other, trying to access the same resource
Producer/Consumer problem
Starvation/Saturation
Producer: Adding data
Consumer: Removes data