---
type: archive
area: archive
status: archived
date: 2026-05-03
created: 2026-05-03
updated: 1980-01-01
tags:
  - archive
---
### Threads
- Java Thread is an execution path
- Definiton of threda is done by creating an instance of Runnable Interafce
- Thread class already implements runnable interafce.
- If a thrad started earlier doesn't mean that he will finish earlier.

![[img-20240816-212814-9255287d.png]]

### Thread Life Cycle
- Change of state of thread is not changed immediately until it find cpu time to run thread

![[img-20240816-213121-06a6b8a1.png]]
- When changing from waiting -> Runnable there is quite amount of time when thread became blocked until it find cpu time to change it to running.
### Block Thread
- Monitor object helps to coordinate the order of execution of threads.
- Blocking thread is stopping them
- Monitor allows threads to enter to block state any thing can be monitor (class , object)
- if a block a code is marked synchronized only on thread is allowed to enter this block or method for a given monitor
- if something is **synchronized** the threads should queue on this thing, only one thread can enter that code for a given monitor.
### Thread properties:
- wait() mathod send thread a signal to go waiting state
- notify() method wakes up the thread that who are waiting against this monitor  (stochasticaly).
- notifyall() wakes up all waiting threads against this monitor.
- Deamon thread are threads that run in background that never finish until the main method exits (jvm exits).
- **JVM** exits when the only remaining threads still running are all daemon threads.
- In another way jvm exit when all user threads finishes.
- .join() tells our thread to wait until other thread finished and then resume.
### Executors:
- Types of Executor service

![[img-20240818-170807-5bbed33e.png]]
- Executor service will manage for us executing task in threads
- Instead of using **Runnable** interface we can use Callable interface that returns somethig object in stead of Runnable that have return type of void
- Callable is launched by submit method.
- Call method of Callable interface with submit method returns a value.
- submit method returns Futur object the return value by call method is stored in Futur object until you get it with .get() method
### Thread Safety
#### Locking Problems:
- Starvation is when Thread waiting for a resource blocked by another busy thread.
- Livelock  a thread form an indefinite loop expecting confirmation of completion from each other.
- DeadLock two pr more threads are blocked forever waiting for each other.
<mark style="background: #FF5582A6;">- To avoid  them use less synchronizition.</mark>
- Stacks of different threads  are hidden
- Heap is shared between Threads.
- Local variables and method arguments are thread-safe
- Thread Stack is isolated
- if object is on the Heap it can be accessed by Threads unless if the object is immutable.
- 2 Fixes we can have: 
	- Volatile keyword
	- Non Blocking Atomic Actions

- <mark style="background: #FFB86CA6;">Volatile</mark> force javac to no cache locally a variable and use it from the heap where it's shared.
- Atomic is guranteed to be performed by a thread without an interruption.
- Actions performed by a CPU in single cycle are by default atomic.
- Synchronised lists, CopyOnWriteArrayLists<>() used to skip problems that we face when we use threads with lists.
- Atomic variables all of them are volatile.
- Locking Mechanism like readlock() or writelock() can prevent problems of concurrent writing in a list for example.
- Write lock prevents other threads from concurrently modifying the object.
### Type of tasks:
	- CPU Bound Tasks (computationnaly intensive)
	- IO Bound Taks (i/o intensive) (takes a thread and don't use t that much)
### Virtual threads 
- are lightweight threads.
- Efficiently serves IO bound concurrency tasks
- Use less heap memory fro metadata
- Virtual threads are mapped from fragment of OS threads.
- Virtual threads are used like other os threads. 
- To make vars immutable we need to make them final
###### Notes:
A Future object represents the result returned by a Callable object and can also represent the completion status of a Runnable task. The ExecutorService.shutdown() method initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. The ExecutorService.shutdownNow() method attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. Note that nether one of these operations is concerned with the time it takes to complete a task.