Skip to content

Makiato1999/Java-Multithreading

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Java-Multithreading

Multithreading, Concurrency & Parallel programming in Java

User Thread vs. Daemon Thread

User Thread:

  1. Runs in the foreground and performs important tasks.
  2. JVM waits for all user threads to complete before it shuts down.

Daemon Thread:

  1. Runs in the background.
  2. 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.

InterruptedException and Thread.join()

  1. 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.
  2. 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 and Throughput

  1. 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.
  2. 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.

Stack

  1. All variables belong to the thread executing on that stack
  2. Statically allocated when the thread is created
  3. The stack's size is fixed, and relatively small (platform specific)
  4. If our calling hierarchy is too deep, we may get an StackOverflow Exception. (Risky with recursive calls)

Stack and Heap

  1. stack (exclusive): local primitive types, local references
  2. heap (shared): objects, class members, static variables

Concurrency

  • two threads sharing the items counter
  • both threads are reading and modifying that counter in the same time
  • the operation were not atomic

About

Multithreading, Concurrency & Parallel programming in Java

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages