Multithreading Flashcards
Thread vs Runnable, run() vs start()
In java concurrency, we have two ways to do this, first to extend Thread and second is to implement Runnable,
We should override or implement the run() method in both cases.
in order to begin the process in the new thread we need to hit the start() method.
Synchronization of java blocks and methods
In Java Synchronized keywords can be used on methods or wrap a block of codes.
It tells the compiler that one and only one thread is allowed to enter this block of code at the same time. and all variable that are used in this block will flushed and updated directly on the main memory
Explain usage of the couple wait()/notify()
Difference between sleep and wait
Atomic operations
Atomic includes both Volatile and Synchronized concepts, which means you guarantee Mutual exclusion and visibility.
The most common Atomic use cases are AtomicInteger, AtomicLong, AtomicBoolean, and AtomicReference.
Atomic have to two methods get/set which read/write from main memory, and only one thread can do it at the same time.
Another use case is compare.
What does it mean Volatile keyword
Volatile variables are never cached by processors and are read directly from the main memory.
Volatile can only implement on field declaration.
Volatile make sure that variable is visible for all threads but it’s only changeable with the responsible thread. which means other treads can’t update the variable.
java.util.concurrent.*, what utils do you know
ExecutorService: runs asynchronous tasks and manages a pool of threads; all threads belonging to the pool will be reused for new tasks;
Executor methods: newFixedThreadPool(), shutdown(), shutdownNow(), awaitTermination()
Callable and Future: we can send a test inside a Callable Function, and then assign an executor in Future. and then be able to wait for the result of the callable by calling the future.
Thread local, what for are they needed
Does child thread see the value of parent thread local
Deadlock definition plus example
Livelock definition plus example
Starvation definition plus example
Race condition definition plus example
Garbage definition plus example
Execution order