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

5.6 Pausing Execution With Sleep Interrupts

The document explains how to control thread execution in Java using sleep() and interrupts. It covers pausing execution with Thread.sleep(), handling interrupts with the interrupt() method, checking if a thread is interrupted using isInterrupted(), and gracefully stopping a thread during long-running operations. Key points include the behavior of sleep() and interrupt() methods, as well as handling InterruptedException.

Uploaded by

Vedaang Sharma
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)
3 views4 pages

5.6 Pausing Execution With Sleep Interrupts

The document explains how to control thread execution in Java using sleep() and interrupts. It covers pausing execution with Thread.sleep(), handling interrupts with the interrupt() method, checking if a thread is interrupted using isInterrupted(), and gracefully stopping a thread during long-running operations. Key points include the behavior of sleep() and interrupt() methods, as well as handling InterruptedException.

Uploaded by

Vedaang Sharma
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/ 4

5.

6 Pausing Execution with Sleep, Interrupts

Java provides mechanisms for controlling thread execution using sleep() and interrupts.
These are useful for pausing execution, managing concurrency, and handling
interruptions in multi-threaded applications.

1. Pausing Execution with sleep()


The Thread.sleep(milliseconds) method pauses the execution of a thread for a specified
time.

Example: Using sleep()


class SleepExample extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread running: " + i);
try {
Thread.sleep(1000); // Pause for 1 second
} catch (InterruptedException e) {
System.out.println("Thread interrupted!");
}
}
}
}

public class SleepDemo {


public static void main(String[] args) {
SleepExample t1 = new SleepExample();
t1.start();
}
}
✅ Output (Pauses for 1 second between iterations):

Thread running: 1
(Thread pauses for 1 second)
Thread running: 2
(Thread pauses for 1 second)
...
🚀 Key Points:
 sleep() temporarily pauses the thread, allowing other threads to execute.
 It does not release locks, so it can still hold resources.
 Throws InterruptedException if another thread interrupts it while sleeping.

2. Handling Interrupts in Threads


Threads can be interrupted using the interrupt() method. This is useful for gracefully
stopping long-running threads.
Example: Interrupting a Sleeping Thread
class InterruptedSleep extends Thread {
public void run() {
try {
for (int i = 1; i <= 5; i++) {
System.out.println("Running: " + i);
Thread.sleep(2000); // Sleep for 2 seconds
}
} catch (InterruptedException e) {
System.out.println("Thread was interrupted while sleeping!");
}
}
}

public class InterruptExample {


public static void main(String[] args) {
InterruptedSleep t1 = new InterruptedSleep();
t1.start();

try {
Thread.sleep(3000); // Main thread waits for 3 seconds
t1.interrupt(); // Interrupt t1 while sleeping
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
✅ Output (Thread gets interrupted while sleeping):
Running: 1
Running: 2
Thread was interrupted while sleeping!
🚀 Key Points:
 interrupt() signals a thread that it should stop.
 If a thread is sleeping or waiting, an InterruptedException is thrown.
 If a thread is not sleeping, interrupt() only sets a flag (isInterrupted()).

3. Checking if a Thread is Interrupted


Threads can check if they have been interrupted using isInterrupted().
Example: Checking isInterrupted()
java
CopyEdit
class CheckInterrupt extends Thread {
public void run() {
while (!isInterrupted()) {
System.out.println("Thread is running...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread interrupted! Stopping execution.");
break;
}
}
}
}

public class CheckInterruptDemo {


public static void main(String[] args) {
CheckInterrupt t1 = new CheckInterrupt();
t1.start();

try {
Thread.sleep(3000); // Main thread waits for 3 seconds
t1.interrupt(); // Interrupt the thread
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
✅ Output:
Thread is running...
Thread is running...
Thread is running...
Thread interrupted! Stopping execution.
🚀 Key Points:
 isInterrupted() returns true if the thread was interrupted.
 If an InterruptedException occurs, the interrupt flag is cleared.

4. Interrupting a Thread Without sleep()


If a thread is performing a long-running operation, it can check isInterrupted() to stop
itself.
Example: Gracefully Stopping a Thread
class LongTask extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
if (isInterrupted()) {
System.out.println("Thread interrupted! Exiting...");
break;
}
System.out.println("Processing: " + i);
}
}
}

public class LongTaskDemo {


public static void main(String[] args) {
LongTask t1 = new LongTask();
t1.start();
try {
Thread.sleep(50); // Let the thread run for a short time
t1.interrupt(); // Interrupt the thread
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
✅ Output (Thread stops before reaching 100):
Processing: 1
Processing: 2
...
Thread interrupted! Exiting...

You might also like