https://github.com/m-thirumal/java-concurrency
Repository for Learning and quick reference for Java concurrency
https://github.com/m-thirumal/java-concurrency
java multithreading parallelism
Last synced: 10 months ago
JSON representation
Repository for Learning and quick reference for Java concurrency
- Host: GitHub
- URL: https://github.com/m-thirumal/java-concurrency
- Owner: m-thirumal
- Created: 2020-07-25T08:19:27.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2025-05-17T08:29:49.000Z (about 1 year ago)
- Last Synced: 2025-05-17T09:30:09.721Z (about 1 year ago)
- Topics: java, multithreading, parallelism
- Language: Java
- Homepage: https://m-thirumal.github.io/java-concurrency/
- Size: 277 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# java-concurrency
### What is Process?
* A process is a `unit of work` in the Operating System
* A process is a instance of program loaded in to memory
* A process can have multiple thread running concurrently/parllel, sharing the same memory and resource of process.
## What is Thread
* A thread is the smallest unit of execution with in the process.
### What is Preemptive & Non-Preemptive?
**Preemptive** is non-blocking task.
**Non-Preemptive** is blocking task.
# Concurrency
- Performing multiple task within same time frame but not necessarily executing at the exact same moment.
Concurrency in a CPU is achieved through `context switching`.
#### How it works
* Context Saving: When CPU switches from one task to another, it saves the current task's state in the memory(Eg: Program counter, registry..)
* Context Loading: The CPU then loads the context of the next task and continues executing it.
* Rapid Switching: The CPU repeats this process, switching between tasks so quickly that it seems like they are running simultaneously.
# Parallelism
- Executing multiple task at exact time by utilizing the multiple cores or processors.
- To achieve parallelism, an application divides its tasks into smaller, independent subtasks. These subtasks are distributed across multiple `CPUs, CPU cores, GPU cores, or similar processing units`, allowing them to be processed in parallel.
- [Basic Understanding](src/main/java/in/thirumal/parallelism/Definition.md)
```java
IntStream.range(0, 100).parallel().forEach(i -> {
System.out.println(Thread.currentThread().getName() + " processing number: " + i);
});
```
Output:
```bash
ForkJoinPool.commonPool-worker-6 processing number: 40
ForkJoinPool.commonPool-worker-6 processing number: 41
ForkJoinPool.commonPool-worker-6 processing number: 42
ForkJoinPool.commonPool-worker-2 processing number: 90
ForkJoinPool.commonPool-worker-4 processing number: 15
main processing number: 65
ForkJoinPool.commonPool-worker-3 processing number: 56
ForkJoinPool.commonPool-worker-5 processing number: 43
ForkJoinPool.commonPool-worker-6 processing number: 37
ForkJoinPool.commonPool-worker-2 processing number: 91
ForkJoinPool.commonPool-worker-1 processing number: 31
ForkJoinPool.commonPool-worker-4 processing number: 16
ForkJoinPool.commonPool-worker-7 processing number: 6
......
```
# Synchronization
Synchronization is the coordination or control of threads to ensure the `consitency` when acessing shared resources.
* `Why it’s needed:`
In concurrent/parallel task, shared resources(like variable/data structures) might be accessed or modified by multiple threads. Without `Synchroniztion`, race conditions can occur, leading to the incorrect result.
[Synchronize.java](src/main/java/in/thirumal/Synchronize/Synchronize.java ':include :type=code')
### Main Concepts
* [Thread Life Cycle](ThreadLifecycle.md)
* [Basic](src/main/java/in/thirumal/t1basic)
* [How to decide the number of thread](docs/How%20to%20decide%20number%20of%20threads.md)
* [Synchronize](src/main/java/in/thirumal/t2Synchronize)
* [Lock](src/main/java/in/thirumal/t2lock/lock.md)
* [Pool-Executor](src/main/java/in/thirumal/t2poool)
* [ThreadLocal](src/main/java/in/thirumal/t1threadlocal)
* [DeadLock](src/main/java/in/thirumal/t5deadlock)
* [Seamaphore](src/main/java/in/thirumal/t6semaphore)
* [Callable & Future](src/main/java/in/thirumal/t1callablefuture)
* [Fork Join](src/main/java/in/thirumal/forkjoin)
* [Complete Future](src/main/java/in/thirumal/t1completablefuture/Basic.md)
* [Thread safe Data Structure](src/main/java/in/thirumal/t1threadsafedatastructure)
* [Reactive](src/main/java/in/thirumal/t1reactive)