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
Explain why multithreaded programs are non-deterministic
Becauses every thread has its own path of execution and we cannot controll the flow of execution. The output can be different every time we run, despite the same input.
Will a thread automatically start upon creation?
No, the thread has to be started after being created
What is a “join”-method?
Join is a synchronization method that blocks the calling thread (that is, the thread that calls the method) until the thread whose Join method is called has completed
Why is “join”-method used?
Join is used when we want one thread to wait for the completion of another thread
How does the “join”-method work if we want one thread to wait for completion of another thread?
The join method of a thread is used to let the caller method wait until the threads whose join method is called is terminated.
When is the first and only mandatory thread creating?
at run-time, its job is to keep the application running