0% found this document useful (0 votes)
4 views8 pages

Unit Iv - Java

This document provides an overview of threads in Java, including how to create, manage, and synchronize them. It discusses the thread lifecycle, methods for stopping and blocking threads, exception handling, thread priorities, and synchronization mechanisms. Understanding these concepts is essential for developing efficient multithreaded applications in Java.

Uploaded by

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

Unit Iv - Java

This document provides an overview of threads in Java, including how to create, manage, and synchronize them. It discusses the thread lifecycle, methods for stopping and blocking threads, exception handling, thread priorities, and synchronization mechanisms. Understanding these concepts is essential for developing efficient multithreaded applications in Java.

Uploaded by

samsung2025.srgi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT IV – Threads in JAVA

Creating threads in java

Thread can be referred to as a lightweight process. Thread uses fewer resources to create and
exist in the process; thread shares process resources. The main thread of Java is the thread that is
started when the program starts. The slave thread is created as a result of the main thread. This is
the last thread to complete the execution.

A thread can programmatically be created by:


 Implementing the java.lang.Runnable interface.
 Extending the java.lang.Thread class.
 Using Lambda Expressions
 Using ExecutorService

You can create threads by implementing the runnable interface and overriding the run() method.
Then, you can create a thread object and call the start() method.

In Java, threads can be created using two primary methods:

1. Extending the Thread Class:


 Create a class that inherits from the java.lang.Thread class.
 Override the run() method, placing the thread's execution logic within.
 Instantiate the class and invoke the start() method to initiate the thread.

class MyThread extends Thread {

@Override

public void run() {

System.out.println("Thread running");

public class Main {

public static void main(String[] args) {

MyThread thread = new MyThread();

thread.start();

2. Implementing the Runnable Interface:

 Create a class that implements the java.lang.Runnable interface.


 Implement the run() method, defining the thread's task.
 Create a Thread object, passing an instance of the Runnable class as an argument.
 Call the start() method on the Thread object.

class MyRunnable implements Runnable {

@Override

public void run() {

System.out.println("Thread running");

public class Main {

public static void main(String[] args) {

MyRunnable runnable = new MyRunnable();

Thread thread = new Thread(runnable);

thread.start();

Key Points:
 start() vs run(): The start() method creates a new thread and executes the run() method in
that new thread. Calling run() directly executes the code within the current thread.
 Thread Lifecycle: Threads go through several states, including new, runnable, running,
blocked/waiting, and terminated.
 Main Thread: Every Java program has a main thread that starts execution.
 Multithreading: Java supports multithreading, enabling concurrent execution of different parts
of a program.
 Thread Priorities: Threads can have priorities (1-10), influencing their scheduling.
 Concurrency Issues: Sharing resources between threads can cause issues. Synchronization
mechanisms may be required.

Stoping and blocking thread


Stopping and blocking threads are crucial aspects of managing concurrency in Java. Here's a
breakdown of how they work:

Stopping a Thread

 Volatile Flag:
A common way to stop a thread is by using a volatile boolean flag. The thread checks this flag in its
loop and exits when the flag is set to true. Volatile ensures that changes to this flag are visible
across threads.
 Interrupt Method:
The interrupt() method sets an interrupt flag on the thread. If the thread is in a blocking state (e.g.,
sleeping or waiting), it throws an InterruptedException. Otherwise, the thread can check its interrupt
status using Thread.currentThread().isInterrupted() and exit gracefully.
 ExecutorService:
When using thread pools, shutdown() and shutdownNow() methods of ExecutorService are used to
stop threads. shutdown() allows the threads to finish their current tasks,
while shutdownNow() attempts to stop all actively executing tasks.

Blocking a Thread

 Synchronized Blocks:
When a thread encounters a synchronized block or method, it attempts to acquire a lock on the
object associated with the synchronized block/method. If the lock is held by another thread, the
thread goes into the BLOCKED state and waits until the lock is released.
 Wait/Notify:
Threads can also be blocked using wait(), wait(long), or wait(long, int) methods of the Object
class. These methods cause the thread to release the lock on the object and wait until notified by
another thread using notify() or notifyAll().
 Sleep Method:
The sleep(long) method pauses the execution of the current thread for a specified time.
 Join Method:
The join() method causes one thread to wait for another thread to complete.

Life Cycle of a Thread in java


Here is the information about the life cycle of a thread in Java:

The life cycle of a thread in Java involves several stages, each representing a different state of
execution. These states are defined by the Thread.State enum and include:

 New:
This is the initial state when a thread object is created using the new keyword, but it has not yet
started execution. At this stage, the thread is not alive.
 Runnable:
After calling the start() method on the thread object, it enters the Runnable state. In this state, the
thread is eligible to run and is waiting for its turn to be scheduled by the thread scheduler.
 Running:
The thread scheduler selects a thread from the runnable pool and assigns it to the CPU for
execution. During this state, the thread's code is actively being executed.
 Blocked/Waiting:
A thread may enter the blocked or waiting state when it is waiting for some external event to
occur. This can include:
 Waiting for a monitor lock to enter a synchronized block or method.
 Waiting for another thread to complete a specific action indefinitely.
 Waiting for I/O resources to become available.

 Timed Waiting:
Similar to the waiting state, but the thread waits for a specific period. This occurs when the thread
uses methods like sleep() or wait() with a timeout.
 Terminated:
This is the final state of a thread when its execution is completed. A thread may terminate naturally
by exiting the run() method or due to an exception. Once terminated, a thread cannot be restarted.
The thread scheduler is responsible for managing the transition of threads between the Runnable and
Running states. The thread's priority can influence how often it gets scheduled, with higher priorities
having a greater chance of being selected.
Threads can be created by: Extending the Thread class and Implementing the Runnable interface.
Understanding the thread life cycle is important for managing concurrency and ensuring efficient
multithreaded applications in Java.
Thread Exception

1. Exception and Exception handling with threads


Here, a new thread is created in the class which is extending the thread class in which run() method
is overridden. This invokes the entry point of the new thread created in the class which was
extending the thread class. Further, start() method is used to start and run the thread in the
program.

// Java program to Use exceptions with thread

// Importing Classes/Files

import java.io.*;

// Child Class(Thread) is inherited from parent Class(GFG)

class DEMO extends Thread {

// Function to check if thread is running

public void run()

System.out.println("Thread is running");

// Using for loop to iterate

for (int i = 0; i < 10; ++i) {

System.out.println("Thread loop running " + i);

// Main Driver Method

public static void main(String[] args)

// Try-catch block to detect exception

try {

// Creating new thread

DEMO ob = new DEMO ();

throw new RuntimeException();

// Catch block to handle exception

catch (Exception e) {

// Exception handler
System.out.println(

"Another thread is not supported");

Output:
Another thread is not supported

2. Exception handling with sleep method(): sleep() method of thread class is used where there is
a demand to sleep the thread for a particular period of time for the proper workflow of the code.

Syntax:
public static void sleep(long milliseconds) ; // generally used
public static void sleep(long milliseconds, int nanoseconds) ; // used to illustrate the precision
only

Thread Priority in JAVA


Thread priority in Java is a mechanism that allows the programmer to influence the order in which
threads are executed. Each thread has a priority value, which is an integer between 1 and 10. The
default priority is 5.
The Thread class defines three constants for thread priorities: MIN_PRIORITY (1),
NORM_PRIORITY (5), and MAX_PRIORITY (10).
The thread scheduler uses these priority values to determine which thread should be given CPU
time. Higher priority threads are generally given preference over lower priority threads. However, the
actual behavior of the thread scheduler is dependent on the underlying operating system and JVM
implementation.
It is important to note that thread priorities are not a guarantee of execution order. The scheduler can
still choose to run a lower priority thread over a higher priority thread in certain situations. Additionally,
relying too heavily on thread priorities can lead to unpredictable behavior and is generally not
recommended for critical tasks.
Here is a code example demonstrating how to set thread priority:
public class ThreadPriorityExample {

public static void main(String[] args) {

Thread thread1 = new Thread(() -> {

System.out.println("Thread 1 running");

});

Thread thread2 = new Thread(() -> {

System.out.println("Thread 2 running");
});

thread1.setPriority(Thread.MIN_PRIORITY);

thread2.setPriority(Thread.MAX_PRIORITY);

thread1.start();

thread2.start();

Thread Synchronization in JAVA

Thread synchronization in Java is a mechanism that controls the access of multiple threads to shared
resources, ensuring that only one thread can access a critical section of code at a time, preventing
data inconsistency and race conditions. It is essential in multithreaded environments to prevent
thread interference and maintain data integrity.
Here's how synchronization works in Java:

1. Synchronized Keyword:
 The synchronized keyword is the primary way to achieve synchronization in Java.
 It can be applied to methods or blocks of code.
 When a thread enters a synchronized method or block, it acquires a lock on the object associated
with that method or block.
 Only one thread can hold the lock at a time, ensuring exclusive access to the synchronized code.
 Other threads attempting to enter the synchronized section will be blocked until the lock is
released.
2. Synchronized Methods:
 When a method is declared as synchronized, the entire method is protected by the lock
associated with the object on which the method is called.
 Only one thread can execute a synchronized method of an object at a time.
3. Synchronized Blocks:
 Synchronized blocks allow you to synchronize only a specific portion of code, rather than the
entire method.
 They use a lock object, which can be any object, to control access to the synchronized section.
 The syntax is synchronized(lockObject) { /* synchronized code */ }.
4. Implicit Locks:
 Every object in Java has an associated implicit lock, also called a monitor.
 When a thread enters a synchronized method or block, it acquires this implicit lock.
 The lock is automatically released when the thread exits the synchronized section, whether
normally or due to an exception.
5. Importance of Synchronization:
 Prevents race conditions: Ensures that shared resources are accessed and modified by only one
thread at a time, avoiding unpredictable outcomes.
 Maintains data consistency: Prevents data corruption that can occur when multiple threads access
and modify shared data concurrently.
 Ensures thread safety: Makes code safe for use in a multithreaded environment.
6. Challenges of Synchronization:
 Performance overhead: Synchronization can introduce performance overhead due to the locking
and blocking of threads.
 Deadlocks: Improper use of synchronization can lead to deadlocks, where threads are blocked
indefinitely waiting for each other to release locks.

You might also like