Multithreading, Concurrency & Parallel programming in Java
- Runs in the foreground and performs important tasks.
- JVM waits for all user threads to complete before it shuts down.
- Runs in the background.
- JVM does not wait for daemon threads to finish. Once all user threads are done, the JVM will terminate the program, which also terminates any running daemon threads.
- The join() method can throw an InterruptedException. This happens if the thread that is waiting (i.e., the one that calls join()) is interrupted while it is waiting.
- The reason for throwing InterruptedException is to signal that something has disrupted the thread's waiting process. Since a thread that calls join() can potentially be waiting for a long time, it’s possible that some other part of the program might want to stop this waiting thread. Such as:
- Another thread needs this waiting thread to do something urgent.
- Application shutdown, requiring all threads to stop as soon as possible.
- Latency is about how fast a single request or action is completed, whereas Throughput is about how many requests or actions can be completed in a unit of time.
- Trade-offs: There is often a trade-off between latency and throughput:
- Increasing throughput might involve processing tasks in bulk, which could increase the latency for individual tasks.
- Reducing latency often means ensuring individual tasks are executed faster, but this may reduce the overall throughput if fewer tasks are executed concurrently.
- All variables belong to the thread executing on that stack
- Statically allocated when the thread is created
- The stack's size is fixed, and relatively small (platform specific)
- If our calling hierarchy is too deep, we may get an StackOverflow Exception. (Risky with recursive calls)
- stack (exclusive): local primitive types, local references
- heap (shared): objects, class members, static variables
- two threads sharing the items counter
- both threads are reading and modifying that counter in the same time
- the operation were not atomic