Concurrency Flashcards
What is concurrency
Perform multiple processes at same time including the potential interaction between them
What are threads
Lightweight processes
exist within a process
Multithreading vs multitasking
Multithreading- shared memory space (each thread has its own local variables in the local memory.
What is non determinism?
We dont know when the OS will schedule threads.
Outputs can vary.
What is liveness?
We need to know that code performs correctly in time.
What is race conditions?
When two threads try to access/change data at the same time it’s known as a race condition
Fix with synchronisation
Atomic actions are…
self contained, they either happen or they don’t. cant be stopped in middle.
Read and writes are atomic for all variables declared volatile.
What is deadlock?
Two or more threads are blocked forever, waiting for each other. Deadlock occurs when multiple threads need the same locks but obtain them in different order.
What is starvation?
Where a thread can’t gain regular access to shared resources because another thread is frequently calling the object’s methods.
What is livelock?
A little like deadlock except that it occurs when two threads are too busy responding to one another to do work
wait()
Waits for a condition to occur, must call from synchronised method/block (releases the lock)
notify()
Notifies a thread in wait() that the condition has occured, must call from synchronised method/block.
sleep()
Does not need to occur in a synchronised block
Retains the monitor (lock) when called and sacrifices the remainder of its timeslice
A sleeping thread can only be woken up by an interruption (not by notify)
5 states of thread
New Runnable Running Waiting/Sleeping/Blocking Dead
yield()
yield() causes the currently executing thread object to pause and allow other threads to execute.
If no other threads of the same priority need to execute, execution of this thread continues
Identifies the current thread as doing something not particularly important