What is Thread in Java - CodeByAkram

What is Thread in Java

What is Thread in Java?


Java Thread is an independent path of execution within a program which can run in parallel with other existing Threads.

Now lets talk about Process.

A process is a self contained execution environment and it can be seen as a program. However a program itself contains multiple processes inside it. Java runtime environment runs as a single process which contains different classes and programs as processes.

So we can say that, thread can be light weight process because it require less resources and also multiple threads can share the same resource. Java provides built-in support for multi-threaded programming.

Let’s summarize  in points:

1. The main purpose of multi-threading is to provide simultaneous execution of two or more parts of a program. Each such part of a program called thread.

2. Threads are lightweight processes, they share the common memory space. 

3. In Multi threaded environment, programs that are benefited from multi-threading, utilize the maximum CPU time so that the idle time can be kept to minimum.

Now lets talk about the state of a thread. Thread can be in one of the following states:-

NEW – A thread that has not yet started is in this state.
RUNNABLE – A thread executing in the Java virtual machine is in this state.
BLOCKED – A thread that is blocked waiting for a monitor lock is in this state.
WAITING – A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING – A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED – A thread that has exited is in this state.
A thread can be in only one state at a given point in time.








No comments:

Post a Comment