0% found this document useful (0 votes)
51 views4 pages

Understanding Java Threading Basics

The document explains threading in programming, particularly in Java, highlighting that a thread is a single sequential flow of control within a program. It describes the advantages of multithreading over multiprocessing, such as lightweight nature and shared address space, and outlines how to create threads by extending the Thread class or implementing the Runnable interface. Additionally, it details the life cycle of a thread, which includes five states: New, Runnable, Running, Non-Runnable (Blocked), and Dead.

Uploaded by

Navneet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views4 pages

Understanding Java Threading Basics

The document explains threading in programming, particularly in Java, highlighting that a thread is a single sequential flow of control within a program. It describes the advantages of multithreading over multiprocessing, such as lightweight nature and shared address space, and outlines how to create threads by extending the Thread class or implementing the Runnable interface. Additionally, it details the life cycle of a thread, which includes five states: New, Runnable, Running, Non-Runnable (Blocked), and Dead.

Uploaded by

Navneet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Threading

All programmers are familiar with writing sequential programs. We have probably written a
program that displays "Hello World!", or sorts a list of names, or computes a list of prime numbers.
These are sequential programs: each has a beginning, an execution sequence, and an end. At
any given time during the runtime of the program there is a single point of execution.
A thread is similar to the sequential programs described above: a single thread also has a
beginning, an end, a sequence, and at any given time during the runtime of the thread there is a
single point of execution. However, a thread itself is not a program. It cannot run on its own, but
runs within a program.
Definition: A thread is a single sequential flow of control within a program.
Java is a multithreaded application that allows multiple thread execution at any particular time. In a
single-threaded application, only one thread is executed at a time because the application or
program can handle only one task at a time. But a multithreaded application allows performing
more than one task at a time.

Multithreading refers to two or more tasks executing concurrently within a single program. A thread
is an independent path of execution within a program. Many threads can run concurrently within a
program. Every thread in Java is created and controlled by the [Link] class. A Java
program can have many threads, and these threads can run concurrently, either asynchronously
or synchronously.
Multithreading has several advantages over Multiprocessing such as;
 Threads are lightweight compared to processes
 Threads share the same address space and therefore can share both data and code
 Context switching between threads is usually less expensive than between processes
 Cost of thread intercommunication is relatively low that that of process intercommunication
 Threads allow different tasks to be performed concurrently.

In java we can create a thread either


i) Extending the Thread class of [Link] package or by
ii) Implementing the Runnable interface

Extending the Thread class: The Thread class itself implements Runnable interface. A class
can inherit the Thread class and override the run () method according to its need.
public class HelloThread extends Thread
{
public void run ()
{
[Link] ("Hello from a thread!");
}
public static void main (String s [])
{
HelloThread t=new HelloThread ();
[Link] ();
}
}

Life cycle of a Thread


A thread can be in one of the five states in the thread. According to sun, there are only 4 states
new, runnable, non-runnable and terminated. There is no running state. But for better
understanding the threads, we are explaining it in the 5 states. The life cycle of the thread is
controlled by JVM. The thread states are as follows:
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Dead

Above-mentioned stages are explained here:


 New: A new thread begins its life cycle in the new state. The thread is in new state when
we create an instance of Thread class but before the invocation of start () method. It
remains in this state until the program starts the thread. It is also referred to as a born
thread.
 Runnable: The thread is in runnable state after invocation of start () method, but the
thread scheduler has not selected it to be the running thread,. but a thread can return to this
state after either running, waiting, sleeping or coming back from blocked state also. On this
state a thread is waiting for a turn on the processor. The start () allocates the system
resources to the thread.
 Running: A thread is in running state that means the thread is currently executing its
task. There are several ways to enter in Runnable state but there is only one way to enter in
Running state: the scheduler select a thread from runnable pool.
 Not Runnable or blocked: A thread becomes Not Runnable when one of these
events occurs:
 Its sleep method is invoked.
 The thread calls the wait method to wait for a specific condition to be satisfied.
 The thread is blocking on I/O.
A thread in blocked state is inactive. That is it is not eligible to processor time. This thread
can be brought back to the runnable state at any time. A thread can go a number of times
from runnable state to blocked state and vice versa in its life cycle.
 Dead: A runnable thread enters the dead state when it completes its task or
otherwise terminates. A thread is in terminated or dead state when its run () method exits. A
thread also goes to the dead state when the stop () is called. A dead thread can never be
restarted.

Common questions

Powered by AI

The JVM controls a thread's lifecycle and manages the transitions between states such as New, Runnable, Running, Non-Runnable (Blocked), and Dead . It allocates CPU time to threads and decides which thread runs at any given moment, ensuring concurrency and efficient utilization of system resources. The thread scheduler, a component of the JVM, handles these tasks, optimizing the application's execution flow and responding to system load conditions .

A thread in Java goes through several states: New, Runnable, Running, Non-Runnable (Blocked), and Dead. A thread starts in the New state when an instance of the Thread class is created. It enters the Runnable state after the start() method is invoked, and the thread scheduler places it in Running state to execute its task. It may become Non-Runnable (Blocked) if it calls sleep(), wait(), or is waiting for I/O operations. Finally, a thread enters the Dead state when it finishes its execution or the stop() method is called .

Multithreading allows multiple tasks to be performed concurrently within a single program, which is not possible in single-threaded applications. Threads are lightweight compared to processes, and context switching between them is less expensive. Threads share the same address space, allowing for efficient data sharing and lower communication costs compared to processes . These attributes make multithreading suitable for applications requiring swift and efficient execution of parallel processes.

A Java thread transitions from the Runnable state to the Non-Runnable (Blocked) state if its sleep() method is invoked, it calls the wait() method to pause execution until a specific condition is satisfied, or it is blocking on I/O operations . To manage these transitions, developers can handle exceptions, synchronize critical sections to prevent resource contention, and design robust conditions for the thread to enter the Runnable state when necessary .

Once a thread enters the Dead state in Java, it completes its execution cycle and cannot be restarted . This state indicates that the thread has finished running its run() method or has been explicitly terminated via a stop() call. Attempting to restart a dead thread results in an IllegalThreadStateException. Proper application design requires planning for thread renewal by creating new instances when necessary instead of attempting to restart a terminated thread .

Threads are lightweight compared to processes, which means they consume fewer resources. Threads share the same address space and can share data and code, whereas processes have separate memory spaces, making inter-process communication more expensive compared to thread communication . Context switching between threads is generally less expensive than between processes, allowing for more efficient execution of multiple tasks within a single program .

Invocation of the start() method allocates system resources to the thread and transitions it from the New state to the Runnable state . In this state, the thread is ready to execute but is waiting for the thread scheduler to select it to enter the Running state, where it performs its task . This method initiates the thread's execution sequence as part of the multithreaded application.

Threads in a Java application execute concurrently because they share the same process memory and are scheduled by the JVM's thread scheduler. This allows them to run independently within the same program scope . The sequence of execution for these threads is determined by the thread scheduler, which allocates processor time based on priority, execution context, and other scheduling policies. The JVM's management of multithreading ensures that threads are executed efficiently and fairly, given resource availability .

In Java, you can implement multithreading by either extending the Thread class or by implementing the Runnable interface . Extending the Thread class involves creating a subclass that overrides the run() method. This approach is simple but less flexible because it prevents the class from extending another superclass. Implementing the Runnable interface is more adaptable as it allows the class to retain the ability to extend another class. Both approaches require the run() method to define the code that constitutes the new thread of execution.

Extending the Thread class in Java restricts a class's design because Java does not support multiple inheritance, meaning the class cannot extend any other superclass. This can limit its reusability and adaptability within complex class hierarchies. To mitigate this limitation, a class can implement the Runnable interface instead. This approach allows the class to extend another superclass if necessary, providing more flexibility and maintainability in the design of Java applications .

You might also like