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()