What is thread in java?
To understand what a thread is, it’s useful to first have a basic understanding of the memory segments in RAM that a program uses. Typically, a program’s memory is divided into four segments:
Code Segment :
- This part of memory contains the executable instructions of a program.
- It is usually read-only to prevent accidental modification of instructions.
- All threads of a process share this segment.
Stack Segment:
- The stack segment is used for function call management. It stores local variables, Methods parameters, return addresses, and the call stack.
- Each thread in a process has its own stack segment to maintain its own function call history and local variables.
class Class2 {
public void method1() {
// Print numbers from 1 to 20 with a delay of 3 seconds between each number
for (int i = 1; i <= 20; i++) {
System.out.print(i + " ");
try {
Thread.sleep(300); // Delay for 3000 milliseconds (3 seconds)
} catch (InterruptedException e) {
}
}
System.out.println(); // Print a new line after printing numbers
}
public void method2() {
// Print numbers from 21 to 40 with a delay of 3 seconds between each number
for (int i = 21; i <= 40; i++) {
System.out.print(i + " ");
try {
Thread.sleep(300); // Delay for 3000 milliseconds (3 seconds)
} catch (InterruptedException e) {
}
}
System.out.println(); // Print a new line after printing number
}
}
public class Class1 {
public static void main(String[] args) {
Class2 obj = new Class2();
// Call method1 from Class2
obj.method1();
obj.method2();
}
}
The provided program defines two classes, Class2
and Class1
, and demonstrates a simple use case where methods from Class2
are invoked sequentially to print numbers with a delay. Here is a detailed explanation of the program:
Class2
Class2
contains two methods, method1
and method2
, each responsible for printing a sequence of numbers with a delay.
method1():
- This method prints numbers from 1 to 20.
- Each number is followed by a space.
- After printing each number, the method pauses for 300 milliseconds (not 3 seconds, as mentioned in the comments; 300 milliseconds is 0.3 seconds).
- The delay is implemented using
Thread.sleep(300)
. - If an
InterruptedException
occurs during the sleep, it is caught and ignored. - After printing the sequence, a newline character is printed to move to the next line.
method2():
- This method prints numbers from 21 to 40.
- The behavior is similar to
method1()
, including the delay and exception handling. - After printing the sequence, a newline character is printed.
Class1
Class1
contains the main
method, which is the entry point of the program.
main():
- An instance of
Class2
is created (obj
). method1
of theClass2
instance (obj
) is called, which prints numbers from 1 to 20.- After
method1
completes,method2
of theClass2
instance (obj
) is called, which prints numbers from 21 to 40.
Execution Flow
Creating an Instance of Class2:
Class2 obj = new Class2();
- An object
obj
ofClass2
is created.
Calling method1():
obj.method1();
- This prints numbers from 1 to 20 with a delay of 300 milliseconds between each number.
Calling method2():
obj.method2();
- This prints numbers from 21 to 40 with a delay of 300 milliseconds between each number.
Example Output
The program will produce the following output with a delay of 300 milliseconds between each number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
- The stack grows and shrinks as functions are called and return.
Heap Segment:
- The heap is used for dynamic memory allocation. This means that memory can be allocated and deallocated at runtime using functions.
- The heap is shared among all threads within a process.
- It is typically larger and more flexible than the stack but requires manual management.