Scheduling Flashcards
How many threads can each CPU run at a time?
1
Is thread scheduling consistently applied or platform dependent?
Platform-dependent.
Java does not require implementations to schedule threads in a particular way.
When must programmers be careful when coding with threads?
When the order of thread execution is vital to the overall program execution.
How does the JVM scheduler deal with threads of the same priority?
First-in-first-out
How many states of threads are there?
4
What are the characteristics of the ‘new’ state?
Thread object created, but thread as such does not yet exist because ‘start’ has not yet been called
What are the characteristics of the ‘runnable’ state?
After start, thread becomes ‘runnable.’
Default state for threads.
Many threads can be in ‘runnable’ state but only one per CPU will be executing
What are the characteristics of the ‘blocked’ state?
If thread calls sleep, wait or join, or if blocked when trying to read data or waiting for access to a lock on a synchronised object, it is moved to the blocked state.
What are the characteristics of the ‘terminated’ state?
When a thread’s run method is finished, it moves to the terminated state.
How does the scheduler determine which thread to run?
From the number of threads in the runnable state, it will be the one with the highest priority.
What will the scheduler do when a higher priority thread enters the runnable state?
The scheduler will pre-empt the currently running executing thread and execute the new thread
How does the schedulers deal with threads of the same priority?
Either run until blocked or terminate or possibly time-sliced.
How does starvation occur?
When a thread never manages to run.
How are shared resources dealt with when threads use them to ensure consistent and correct access to the shared data?
Locks
If a thread with a lower priority started to executed and obtained a lock on a shared resource but was then interrupted by a higher priority thread which also needed access to the locked resource, what would need to occur?
Priority Inheritance
What is meant by task precedence?
Some threads need to execute before others to ensure for the correct execution of the program as a whole
What are timing constraints?
In a real-time system, each thread may have its own deadline, some will take longer than others to complete, some may operate on a periodic basis
What is priority inversion?
Where lower priority threads can execute and terminate before higher priority threads due to locks placed on resources making some threads unable to execute
What is priority inheritance?
When a lower priority thread has a lock on a sync. resource which is required by a higher priority thread, the lower priority thread’s priority is temporarily and silently raised to allow it to finish and release the lock
If threads of the same priority pre-empt each other, what is this known as?
Round-Robin scheduling
Is round-robin scheduling required in JVM?
No
Are Java threads fair?
No, higher priority threads will ensure lower priority ones will not if they are in the same pool
In Java, with no round-robin scheduling, are threads of the same priority guaranteed to pre-emptively time-sliced?
No
What happens if Round-Robin scheduling is used to deal with multiple threads of the same priority?
For all threads to complete it will take the same overall time.
What happens if Round-Robin scheduling is not used to deal with multiple threads of the same priority?
One thread could be finished very early, and another would take a very long time.