Concurrency Flashcards
What are the 6 stages of a threads lifecycle?
Ready to run, running, waiting, sleeping blocked on I/O or synch Dead
What is event dispatch?
The gui thread within java
When does a single thread die?
When its run method returns or .stop is called on it
What class do the .notify, and .wait methods belong to?
Belongs to the object class
What object do the sleep and, start and yield belong to?
Thread object.
When is a class thread safe?
If it behaves correctly when accessed by multiple threads. Regardless of the scheduling or interleaving of those threads by the runtime. With no extra synchronisation on the part of the calling code.
Why should you avoid sharing states between threads wherever possible?
Sharing states leads to race conditions and deadlock?
What are the ways to create a new thread?
Calling .run() on any of the following: A new anonymous thread class, A new thread with a lambda inside. New thread subtype with the run class overridden, New runnable implementation passed as a parameter to a new Thread.
Implementing runnables vs extending threads? Whats the difference.
Inheritance should be used when you want to override existing functionality, what existing behaviour are we trying to override in thread? Instead we seek to extend the existing behaviour by giving it something to run. Furthermore, implementing runnables creates loose coupling in our program and allows our runnable to inherit something else.
What is a race condition?
A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Thread scheduling means that their order is random. The random timing and simultaneous access can result in unexpected results.
What are check then act and read then write.
Neither of these operations are atomic, check then act can make the condition that was checked become outdated midway throughout execution.
Whilst in read then write two threads can read a value at the same time causing the wrong write value.
Why can even primitives behave unexpectedly?
The JVM caches values for periods of time. Meaning that the change in value may not be a publicly available resource.
What is write back caching?
When stuff is placed in the cache and then eventually put into main memory in batches.
What is write through caching?
When the processor is told to put straight from the cache into main memory.
Which is faster out of write through and write back caching? Which is safer?
Write back is faster. Write through is safer.
What is a thread?
A flow of control within a program.
What are the advantages of threads?
- Reactive/realtime system.
- Responsive to user input.
- Concurrent server requests.
- Take advantage of multicores/processors.
- Supports object decoupling
What methods for thread control are deprecated?
.stop, .suspend and .resume. Because they suddenly seize control away from the thread mid execution.