Final Exam Flashcards
Process
Instance of an application running on a system
A process that tries to use memory not allocated to it will generate a ___
Segmentation fault
Each process can run one or more ___
Threads
T/F: Cores can run multiple threads at once
False
Each process is given a ___ to complete its work
Time slice (normally a constant)
T/F: Two processes cannot read from each other’s memory
True
All threads in the same process ___
Share the same memory space
The two ways to have Java execute threads
Executing the main thread
Extending the Thread class
T/F: A thread initiated with the start method will execute separately from the main method
True
Calling the run method instead of the start method on a thread
Will execute it in the main thread instead of its own
Scheduler
Controls when, and if, a thread gets to run. Minimal control over it. Like a black box–unpredictable.
NEW (thread state)
A thread has been created but start not called
RUNNABLE (thread state)
Start called and nothing is preventing thread from running
Running (unofficial thread state)
Thread is executing on a processor
DEAD (thread state)
A thread has finished its execution or has been terminated
To check if a thread is not DEAD state
isAlive()
Threads can be created by ___ Thread or ___ Runnable
Extending. Implementing. Runnable instances must be passed into thread constructors
Pros and cons of extending Thread
Pro:
Less code
Con:
Single inheritance. Run method optional. Can’t be restarted.
Pros and cons of implementing Runnable
Pros:
Multiple inheritance. Must override run. Can be restarted in new thread.
Cons:
More code
When multiple threads are running their output is ___
Interleaved. Some may run on same core, others on other cores.
Busy Wait
Pinging a thread repeatedly to see if it is complete (using isAlive()). Wastes CPU cycles while doing no work.
Threads can relinquish their time in a processor by using ___
Sleep
Sleep can be used to:
Simulate passage of time.
Let other processes work while it waits.
T/F: Upon awaking, a thread will immediately begin running again
False. It must wait to be scheduled.
Exception for sleep
Interrupted Exception. Checked. Can basically be ignored.
Sleep should always be called on ___
The Thread class (not an instance)
Join
Allows threads to queue, running one after the other has finished (used after start)
Joining threads stops them from
Running concurrently
Race condition
When two or more threads are trying to access/update the same shared resource
Context switch
When the active thread changes between retrieving a value and using said value
Shared resources are also known as ___
Critical regions. Unsafe in multi-threaded environments unless protected
A protected critical region is ___
Thread Safe
Resource contention
When two or more threads attempt to manipulate the value of a shared resource at the same time
Non-atomic operation
An operation that is implemented with at least two separate instructions
Synchronized
Keyword used with an object key to create a mutually exclusive lock
T/F: Primitive types can work as keys to synchronize on
False. Must be a reference type/object
If a second thread tries to access a synchronized block using the same object key ___
It will be locked out and enter a blocked state
When a thread exits a critical region, it ___ the lock and a ___ thread will transition to the ___ state
Surrenders. Blocked. Runnable.
Threads trying to access the critical region must have access to the ___. Because of this, it is common to use the ___ itself as the ___.
Object key. Shared resource, key.
Synchronizing the entire run method causes threads to run ___ rather than ___
Sequentially. Concurrently.
To minimize the amount of time threads are blocked, synchronization should be done in a ___ way
Surgical
If you control the object code for a shared resource, one option is to
Synchronize on methods or portions of methods in the object
T/F: Synchronized code runs much slower than code that is not synchronized, even if it is only used by one thread
True.
T/F: Most Java standard libraries are thread-safe
False. Too slow. Most code is for single thread.
Hashtable
Thread-safe version of HashMap. Deprecated. Don’t use.
Polling
Checking if a thread can proceed at certain time intervals. Can cause delay from when job is added and thread checks
Lock.wait()
Pauses execution of the thread indefinitely. Surrenders the lock.
Lock.notify()
Notifies a waiting thread that is waiting on the same lock
Lock.notify() chooses __ thread and moves it to the __ state
One. Blocked.