Chapter 18: 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 system thread?
A system thread is created by the JVM and runs in the background of the application.
For example: the garbage collection is managed by a system thread that is created by the JVM and runs in the background, helping to free memory that is no longer in use.
What is a user-defined thread?
A user-defined thread is one created by the application developer to accomplish a specific task.
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.
Is Runnable a functional interface?
yes
What is the method signature of the interface Runnable?
run()
Does the following code compile?
Runnable sloth = () -> {return;}
The Runnable interface has one abstract method: run(), which takes no arguments and returns void. Therefore, this code is valid.
Does the following code compile?
Runnable sloth = () -> 5;
No. It returns 5 while a Runnable cannot return a value.
Does the following code compile?
Runnable sloth = () -> System.out.println(“Hello World”);
Yes.
What package does the Thread class reside in?
java.lang
Is order of thread execution guaranteed?
Order of thread execution is never guaranteed. It may be executed immediately or delayed for a significant amount of time.
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 is polling?
Polling is the process of intermittently checking data at some fixed interval.
Example: checking to see if a counter variable has reached 100 in a while loop, while another thread is incrementing it.
What exception can Thread.sleep() throw?
InterruptedException.
In what package does the Executor class reside in?
java.util.concurrent
Is order of thread execution with a single-thread executor guaranteed?
Yes, because it can only perform 1 task at a time.
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()