A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables, and a stack which contains the execution history.
in java a thread comprises following things.
1. Thread Id
2. Program counter
3. Java Stack
Java Stack Memory:-
Java Stack memory is used for execution of a thread.
They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Java Stack memory is always referenced in LIFO (Last-In-First-Out) order. Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. As soon as method ends, the block becomes unused and become available for next method.
Stack memory size is very less compared to Heap memory.
Java Stack Example:-
When more than one threads are accessing shared resource:-