Ch18 Concurrency Flashcards
What is a thread?
A thread is the smallest unit of execution that can be scheduled by the operating system.
What is a context switch?
A context switch is the process of storing a thread’s current state and later restoring the state of the thread to continue execution.
What is a thread priority?
A thread priority is a numeric value associated with a thread that is taken into consideration by the thread scheduler when determining which threads should currently be executing.
In java, thread priorities are specified as integer values.
What arguments does the Runnable interface take? And what does it return?
None and nothing.
What package does the Thread class reside in?
java.lang
What are two ways to define a task that a Thread instance will execute?
- Providing a Runnable object or lamda expression to the Thread constructor: new Thread(runnable);
- Create a class that extends Thread and overrides the run() method.
What method is used to start a thread?
start()
What exception can Thread.sleep() throw?
InterruptedException.
In what package does the Executor class reside in?
java.util.concurrent
What method should be called once you have finished using a thread executor?
shutdown()
What does the executor’s shutdown() method do?
Rejects any new task submitted to the thread executor while continuing to execute any previously submitted tasks.
What method can we call to check if shutdown() was called and the thread executor has completed all active tasks?
isTerminated()
What method can we call to check if shutdown() was called on a thread executor?
isShutDown()
What method can we call on a thread executor to attempt to cancel all running and upcoming tasks?
shutdownNow()
What does the thread executor method shutdownNow() do and return?
It will attempt to cancel all running and discard all upcoming tasks, and return a List of all tasks that were submitted but never started.
What thread executor method is available to execute a Runnable and return void? (fire-and-forget)
execute(runnable)
What thread executor method is available to execute a Runnable and return a Future to determine wether the task is complete?
submit(runnable)
What thread executor method is available to execute a Callable and return a Future to determine wether the task is complete?
submit(callable)
What thread executor method is available to execute a list of tasks and waits for all tasks to be complete and returns a list of Future instances?
invokeAll(collection)
What thread executor method is available to execute a list of tasks and waits for any of the tasks to be complete and returns a Future instance of that task?
invokeAny(collection)
What is the advantage of using submit() over execute()?
submit() is equal to execute() except that it has a return object that can be used to track the result.
What Future method is available to check to see if the task was completed?
isDone()
What Future method is available to check to see if the task was cancelled?
isCancelled()
What Future method is available to attempt to cancel a a task? What does it return?
cancel()
Returns true if cancelling was succesful or false if the task could not be cancelled or was completed.
What Future method is available to get the result of a task, while waiting until it is available?
get()
What Future method is available to get the result of a task, with a timeout of 1 second? What happends when the timeout is reached?
get(1, TimeUnit.SECONDS)
it will throw a TimeoutException if the timeout was reached.
What package does the Callable interface reside in?
java.util.concurrent