Java - Concurrency & Multithreading
What are the two ways to create a thread within a process?
1) Create a new class extends type Thread 2) Create a new class to implement the Runnable interface
A Runnable thread transitions to the _______ state when it attempts to perform a task that cannot be completed immediately and it must temporarily wait until that task completes
Blocked
True or False: When a thread issues an input/output request the operating system keeps the thread in the running state until the I/O request completes.
False
What are some specific uses for threads?
Long initiations (read long file) Repetitive or timed tasks (animations) Asynchronous events (event handling such as a mouse click) Multiple Tasks (To do more than one thing at once)
Name the Thread Life Cycle and States
New Runnable Blocked Waiting Timed Waiting Terminated
What are two fundamental problems with multithreading
No knowledge of when a thread will run and multiple threads may try to access the same resource
The Worker class has observable states defining the lifecycle of an object: Name the observable states.
Ready - initial state Scheduled - scheduled for work Succeeded - completed successfully Failed - exceptions occurred Cancelled - interrupted
A thread can be put in waiting state for various reasons e.g. calling it's wait() method. Usually program puts a thread in WAIT state because something else needs to be done prior to what current thread is doing. Once the thread wait state is over, its state is changed to ________ and it's moved back to thread pool.
Runnable
A thread enters the _________ state (sometimes called the dead state) when it successfully completes its task or otherwise terminated due to any error or even it was forcefully killed.
Terminated
Inside processes we utilize _____ to execute code concurrently
Threads
True or False: There is not a reliable means of cancelling tasks in Java, therefore best practive of JavaFX tasks is to periodically check if they have been cancelled - using isCancelled()
True
True or False: the OS hides the Runnable and Running states from the JVM which sees only the Runnable state?
True
The javafx.concurrent package facilitates thread interaction for JavaFX applications. Name the 3 important aspects of this package and what their role is
Worker - interface Task - abstract subclass of Worker Service - executes tasks, subclass of Worker
Tasks are responsible for the implementation of the work done in the _________
background
How do you create a concurrent thread by extending the Thread class?
create a class that extends Thread. override the run() method. To run the class, instantiate the class (creating a new thread) then call start().
How do you create a concurrent thread by using the Runnable interface?
create a class that implements Runnable. Implement the run() method. to run the class, instantiate the class (creating a new thread) then call start().
In order to prevent two, or more threads, attempting to access the same resource, a _____ is put on the resource
lock
Processes are instances of programs which typically run independent to each other. ______ allocated by OS
memory
As soon as you create a new thread, its in a ____ state and remains in the state until the program starts the thread using its start() method. Note: At this point, the thread is not alive and its a state internal to java programming.
new
Java has 3 levels of synchronization, what are they
object level method level block level
Give an example of a class that multithreaded for reading lines
public class ReaderTask extends Task<String>{ protected String call(){ String result = null; // read in data from a file result = in.readLine(); return result; } }
Calling start() method on a thread puts it in a ______ state. At this point, execution control is passed to the OS thread scheduler to finish its execution.
runnable
A task utilizing its cpu time is said to be in a _________ state. When this time expires it returns to the runnable state and the OS assigns another thread to the processor.
running
What two methods can place a thread into a Timed Waiting state
sleep(long millis) wait(long millis)
Sharing resources between threads is done with __________
synchronization
In order to prevent threads collision over resources, methods can be declared as _____, only allowing one thread to call the method of a particular object.
synchronized
A ______ is a path of execution through a program
thread
Once a Task has been created it can be started via a ________
thread