Concurrency/Threading Flashcards
What is concurrency?
Concurrency is the ability to perform multiple processes out-of-order or in partial order, including the potential interaction between them.
In other words, it’s our ability to safely run multiple processes in parallel.
What is a thread?
A thread is almost like a smaller process within a process. They are each allotted their own stack and registers just as if they were separate processes.
What is multithreading?
Multithreading is the ability of the operating system to run threads in parallel.
What is the difference between virtual and physical threading?
Virtual threading in a single processor is where the operating system uses techniques such as fine grain threading to simulate multiple processes running at once.
Physical threading in a multi-core system is where the operating system assigns each thread a core.
What is meant by “non-determinism” in threading?
Non-determinism means that the order of task execution is impossible to be determined. In other words, each thread will be unable to tell which program is starting or finishing last, and will not know if it itself is first or last.
Why do we call start() and not run() in Java?
run() is the interface identifier for the method that is to be executed. Hence, if you call run() directly, it will simply run on the thread it is executed on, and not a new thread.
start() starts a new thread and begins executing run() on that new thread.
How can you stop a thread?
Threads will automatically stop and clean themselves up once they hit the end of the run() block, or if it is returned.
If you want to continue looping infinitely until stated otherwise, you can use isInterrupted() as a while flag to continue executing until a controlling parent thread interrupts or stops it.
What are race conditions?
A race condition is caused when two threads try to access/change data at the same time.
Why do race conditions happen?
Because threading is non-deterministic, if both threads try and do something different to the data, it will be random (or effectively random) which thread starts first, making the outcome (effectively) random each time it is ran.
What does the “synchronized” keyword do in Java?
The synchronized keyword places a ‘lock’ on the method whenever a thread accesses it. A method that has a ‘lock’ on it may not be accessed by another thread. This ‘lock’ remains until the method or synchronized block has finished executing.
What is the condition caused when no member of a group of entities can proceed because each entity is waiting for another member in the group?
Deadlock
What is the condition caused when a thread cannot gain regular access to shared resources because another thread is hogging the memory access?
Starvation
What is the condition caused when two threads are too busy responding to each other to carry out their tasks?
Livelock
An example because this one is complicated: two people bump into each other in a hallway, so they walk back to where they started and then walk back down the same hallway, repeating this in a loop.
Which out of wait() and sleep() will only wake up when interrupted?
sleep().
It cannot be woken up using a notify() or notifyAll().
Which out of wait() and sleep() gives up the thread’s allotted time-slice whenever they are called?
sleep().
If you leave a thread sleeping for too long, the allotted time given by the operating system will get wasted.