Mod 1: Thread properties Flashcards
What is a thread?
(1) A single sequential flow of control within a program
(2) Sequence of instruction
(3) A unit of work that can be executed
What does a thread share with the process and what does it has its own of?
It do share
(1) same memory space
(2) same program code and global data
but has its own
(1) set of registers
(2) stack
Why is using single-threaded applications on modern computers underuse the capacity of of the computer?
Because modern computer often have multiple cores serving as mini-CPU. Using a single-threaded application loads one core with a lot of work, while the other cores stay idle.
What are the four typical states that a thread can have?
(1) Ready
(2) Running/Active
(3) Blocked/Waiting
(4) Terminated
What does a “ready” thread mean?
Created, but not started
What can cause a thread to die?
- It is done with its task
- Exception or other unusual event
When does thread blocking occur?
When a thread is unable to continue its execution because it is waiting for something
Give example for what thread could be “waiting” on
(1) Resources to become available
(2) Conditions to be true
(3) Event to occur
What happens when a thread is blocked?
It becomes temporarily suspended and other threads are allowed to execute
How can a blocked thread resume its work?
By receiving CPU-time again
What is “sleep”?
Timed waiting
Why would we want to put a thread in sleep-mode?
to make processor time available for other threads
What happens if there is no CPU time when the sleep time is over?
Nothing, the thread in sleeping-state need to receive CPU-time before resuming its task
What is the difference between a foreground thread and a background thread?
a foreground thread keep the application alive and the background thread is running “behind the scenes”, this mean that if the foreground thread is terminated and we exit the application, the background thread will die regardless of if they are done with their tasks
What is a foreground thread?
The thread keeping the application alive, JVM cannot exit the application when there is a foreground thread running