What is Thread in Java?

Definition: A thread is a single sequential flow of control inside a program. A thread is like a sequential programs which has a beginning, has a sequence, has an end and has a single point of execution at any given point of time during the runtime of the thread.

Some definitions of thread consider a thread as a lightweight process because it runs within the context of a full-blown program and consume less system resources as compared to process.
Thread is not a program but runs inside a program and multiple threads running inside the same program share same resources and are less insulated from each other than separate processes are. They share memory area, same file handles, and other per-process state.
Like processes, threads are independent and concurrent paths of execution through a program. Each thread has its own stack, its own local variables and its own program counter.

What is multi-threading?

There is no fun in a program which has a single thread running inside, rather its about the use of multiple threads running and performing different task at the same time in a single program.
So, multi-threading is a way to allow multiple activities coexist within a single program. A process can support multiple threads to execute simultaneously and asynchronously to each other. Multiple threads within a process share the same memory address space, which means they have access to the same resources like variables and objects, While this makes it easy for threads to share information with each other, so we must take care to ensure that multiple threads do not interfere with other threads in the same process. Java is the first programming language to explicitly provides multi-threading feature within the language itself rather than treating threading as facility provided by underlying operating system.

Every program in Java use threads

Every Java program has at least one thread – the main thread.
JVM creates the main thread when a Java program get started and calls the program’s main() method within that thread. JVM also creates some background threads which are mostly invisible to the programmer. For example, threads related with garbage collection, object finalization and other JVM housekeeping tasks.

In Java each thread is created and controlled by the Thread class of package java.lang. This class provides the Thread APIs and all generic behavior like starting, sleeping, running, yielding and priority for threads.

Java Professional with rich experience in Java development.
– Designing and developing high-volume, low-latency applications with high-availability and performance.
– Writing well designed, robust and efficient code.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By : XYZScripts.com