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