By Pankaj Kumar and Anish Singh Walia
The Java Thread.sleep()
method is a fundamental tool for controlling thread execution by pausing the current thread for a specified duration. While its basic usage appears straightforward, understanding its advanced mechanics, performance implications, and modern applications—including AI-driven use cases—is crucial for building robust, scalable Java applications.
The Thread.sleep()
method can be used to pause the execution of the current thread for a specified time in milliseconds. The argument value for milliseconds cannot be negative. Otherwise, it throws IllegalArgumentException
.
sleep(long millis, int nanos)
is another method that can be used to pause the execution of the current thread for a specified number of milliseconds and nanoseconds. The allowed nanosecond values are between 0
and 999999
.
In this tutorial, you will learn about Java’s Thread.sleep()
method, including advanced technical insights, performance considerations, AI applications, and modern alternatives for enterprise-grade applications.
Thread.sleep()
is a fundamental tool for controlling thread execution with minimal resource consumptionInterruptedException
properly to ensure graceful application shutdown and responsivenessThread.sleep()
doesn’t release locks and has limited precision due to system timersThread.sleep()
for simple delays, but consider alternatives like ScheduledExecutorService
for complex scheduling needs.Thread.sleep()
Thread.sleep()
WorksThread.sleep()
interacts with the thread scheduler to put the current thread in a wait state for a specified period of time. Once the wait time is over, the thread state is changed to a runnable state and waits for the CPU for further execution. The actual time that the current thread sleeps depends on the thread scheduler that is part of the operating system.
Thread
State ManagementWhen Thread.sleep()
is invoked, the current thread transitions to a TIMED_WAITING
state. This is a crucial distinction from other waiting states:
The thread scheduler, managed by the operating system, determines when the thread will resume execution. This process involves:
Thread.sleep()
Important PointsThread.sleep()
doesn’t lose any monitors or locks the current thread has acquired. This is a critical point that can lead to deadlocks if not properly managed.InterruptedException
is thrown.Understanding the performance characteristics of Thread.sleep()
is essential for building efficient applications:
Thread.sleep()
ExamplesHere is an example program where Thread.sleep()
is used to pause the main thread execution for 2 seconds (2000 milliseconds):
package com.journaldev.threads;
public class ThreadSleep {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
Thread.sleep(2000);
System.out.println("Sleep time in ms = " + (System.currentTimeMillis() - start));
}
}
First, this code stores the current system time in milliseconds. Then it sleeps for 2000 milliseconds. Finally, this code prints out the new current system time minus the previous current system time:
OutputSleep time in ms = 2005
Notice that this difference is not precisely 2000 milliseconds. This is due to how Thread.sleep()
works and the operating system-specific implementation of the thread scheduler.
Here’s a more sophisticated example that demonstrates rate limiting with adaptive sleep duration optimization:
package com.journaldev.threads;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.ThreadLocalRandom;
public class AdvancedThreadSleep {
private static final AtomicLong requestCount = new AtomicLong(0);
private static final AtomicLong totalResponseTime = new AtomicLong(0);
public static void main(String[] args) throws InterruptedException {
// Simulate API rate limiting with adaptive sleep
for (int i = 0; i < 10; i++) {
long startTime = System.currentTimeMillis();
// Simulate API call
simulateApiCall();
long responseTime = System.currentTimeMillis() - startTime;
totalResponseTime.addAndGet(responseTime);
requestCount.incrementAndGet();
// AI-driven adaptive sleep based on response time
long adaptiveSleep = calculateAdaptiveSleep(responseTime);
System.out.println("Request " + (i + 1) + " completed in " +
responseTime + "ms, sleeping for " + adaptiveSleep + "ms");
Thread.sleep(adaptiveSleep);
}
System.out.println("Average response time: " +
(totalResponseTime.get() / requestCount.get()) + "ms");
}
private static void simulateApiCall() throws InterruptedException {
// Simulate variable processing time
Thread.sleep(ThreadLocalRandom.current().nextInt(50, 200));
}
private static long calculateAdaptiveSleep(long responseTime) {
// AI-inspired adaptive algorithm
if (responseTime > 150) {
return 2000; // Longer sleep for slow responses
} else if (responseTime < 100) {
return 500; // Shorter sleep for fast responses
} else {
return 1000; // Default sleep time
}
}
}
Here’s an example demonstrating proper InterruptedException
handling:
package com.journaldev.threads;
public class ThreadSleepWithExceptionHandling {
public static void main(String[] args) {
Thread workerThread = new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println("Working... " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// Restore interrupted status
Thread.currentThread().interrupt();
System.out.println("Thread was interrupted: " + e.getMessage());
// Perform cleanup operations
cleanup();
}
});
workerThread.start();
// Interrupt the thread after 3 seconds
try {
Thread.sleep(3000);
workerThread.interrupt();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private static void cleanup() {
System.out.println("Performing cleanup operations...");
}
}
Here’s an example showing Thread.sleep()
in a microservices context with circuit breaker pattern:
package com.journaldev.threads;
import java.util.concurrent.atomic.AtomicInteger;
public class MicroserviceWithSleep {
private static final AtomicInteger failureCount = new AtomicInteger(0);
private static final int MAX_FAILURES = 3;
private static final long BACKOFF_DELAY = 1000;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
try {
callExternalService();
// Reset failure count on success
failureCount.set(0);
} catch (Exception e) {
int failures = failureCount.incrementAndGet();
System.out.println("Service call failed (" + failures + " failures): " + e.getMessage());
if (failures >= MAX_FAILURES) {
// Circuit breaker: exponential backoff
long backoffTime = BACKOFF_DELAY * (1L << Math.min(failures - MAX_FAILURES, 5));
System.out.println("Circuit breaker activated. Backing off for " + backoffTime + "ms");
Thread.sleep(backoffTime);
} else {
// Simple retry with linear backoff
Thread.sleep(BACKOFF_DELAY * failures);
}
}
}
}
private static void callExternalService() throws Exception {
// Simulate external service call
if (Math.random() < 0.3) { // 30% failure rate
throw new Exception("Service temporarily unavailable");
}
System.out.println("Service call successful");
}
}
In modern AI applications, Thread.sleep()
plays a crucial role in performance testing frameworks. AI systems can dynamically determine optimal sleep durations based on real-time performance metrics:
package com.journaldev.threads;
import java.util.concurrent.atomic.AtomicLong;
import java.util.List;
import java.util.ArrayList;
public class AIPerformanceTesting {
private static final List<Long> responseTimes = new ArrayList<>();
private static final AtomicLong totalRequests = new AtomicLong(0);
public static void main(String[] args) throws InterruptedException {
// AI-driven warm-up phase
performWarmUpPhase();
// Main testing phase with adaptive sleep
for (int i = 0; i < 100; i++) {
long startTime = System.currentTimeMillis();
// Simulate AI model inference
performAIModelInference();
long responseTime = System.currentTimeMillis() - startTime;
responseTimes.add(responseTime);
totalRequests.incrementAndGet();
// AI-optimized sleep duration
long optimalSleep = calculateOptimalSleep(responseTime);
Thread.sleep(optimalSleep);
}
analyzePerformance();
}
private static void performWarmUpPhase() throws InterruptedException {
System.out.println("Starting AI-driven warm-up phase...");
for (int i = 0; i < 10; i++) {
long startTime = System.currentTimeMillis();
performAIModelInference();
long responseTime = System.currentTimeMillis() - startTime;
// AI determines when warm-up is complete
if (isWarmUpComplete(responseTime)) {
System.out.println("Warm-up completed after " + (i + 1) + " iterations");
break;
}
Thread.sleep(100); // Short sleep during warm-up
}
}
private static void performAIModelInference() throws InterruptedException {
// Simulate AI model processing time
Thread.sleep(ThreadLocalRandom.current().nextInt(10, 100));
}
private static boolean isWarmUpComplete(long responseTime) {
// AI logic: warm-up complete when response time stabilizes
return responseTimes.size() >= 5 &&
Math.abs(responseTime - getAverageResponseTime()) < 10;
}
private static long calculateOptimalSleep(long responseTime) {
// AI-inspired adaptive sleep calculation
double averageResponseTime = getAverageResponseTime();
if (responseTime > averageResponseTime * 1.5) {
return 2000; // Longer sleep for slow responses
} else if (responseTime < averageResponseTime * 0.8) {
return 500; // Shorter sleep for fast responses
} else {
return 1000; // Default sleep time
}
}
private static double getAverageResponseTime() {
return responseTimes.stream()
.mapToLong(Long::longValue)
.average()
.orElse(0.0);
}
private static void analyzePerformance() {
double averageResponseTime = getAverageResponseTime();
System.out.println("Performance Analysis:");
System.out.println("Total requests: " + totalRequests.get());
System.out.println("Average response time: " + averageResponseTime + "ms");
System.out.println("95th percentile: " + calculatePercentile(95) + "ms");
}
private static long calculatePercentile(int percentile) {
responseTimes.sort(Long::compareTo);
int index = (int) Math.ceil(percentile / 100.0 * responseTimes.size()) - 1;
return responseTimes.get(Math.max(0, index));
}
}
Java-based deep learning frameworks often use Thread.sleep()
for synchronization and resource management:
package com.journaldev.threads;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.CompletableFuture;
public class DeepLearningFramework {
private static final ExecutorService executor = Executors.newFixedThreadPool(4);
public static void main(String[] args) throws InterruptedException {
// Simulate deep learning training with multiple GPUs
CompletableFuture<Void> gpu1 = CompletableFuture.runAsync(() -> {
try {
trainModelOnGPU("GPU-1", 1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}, executor);
CompletableFuture<Void> gpu2 = CompletableFuture.runAsync(() -> {
try {
trainModelOnGPU("GPU-2", 1200);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}, executor);
// Synchronize training across GPUs
CompletableFuture.allOf(gpu1, gpu2).join();
System.out.println("Training completed successfully");
executor.shutdown();
}
private static void trainModelOnGPU(String gpuId, long baseTime) throws InterruptedException {
for (int epoch = 0; epoch < 10; epoch++) {
long startTime = System.currentTimeMillis();
// Simulate GPU training
simulateGPUTraining();
long trainingTime = System.currentTimeMillis() - startTime;
System.out.println(gpuId + " - Epoch " + epoch + " completed in " + trainingTime + "ms");
// Synchronize with other GPUs using adaptive sleep
long syncDelay = calculateSyncDelay(trainingTime, baseTime);
Thread.sleep(syncDelay);
}
}
private static void simulateGPUTraining() throws InterruptedException {
// Simulate variable GPU processing time
Thread.sleep(ThreadLocalRandom.current().nextInt(100, 500));
}
private static long calculateSyncDelay(long actualTime, long expectedTime) {
// Calculate delay to synchronize with other GPUs
long difference = expectedTime - actualTime;
return Math.max(0, difference);
}
}
Thread.sleep()
Use Cases and PatternsRate limiting is a crucial technique used to control the rate at which a client or user can send requests to a server or access a resource. Its primary purpose is to prevent abuse, ensure fair usage, protect against denial-of-service (DoS) attacks, and manage the load on backend systems. When a client exceeds the predefined request limit within a specific time window, the system can either reject subsequent requests or, more gracefully, introduce a delay.
Thread.sleep()
can be effectively used to implement simple rate-limiting mechanisms. By pausing the current thread’s execution when a rate limit is hit, we can ensure that requests are spaced out over time, preventing a flood of requests that could overwhelm a service. This approach is particularly useful for client-side applications interacting with external APIs that enforce strict rate limits.
package com.journaldev.threads;
import java.util.concurrent.atomic.AtomicLong;
public class RateLimitingExample {
private static final int MAX_REQUESTS_PER_SECOND = 10;
private static final AtomicLong requestCount = new AtomicLong(0);
private static final AtomicLong windowStart = new AtomicLong(System.currentTimeMillis());
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 50; i++) {
if (isRateLimitExceeded()) {
long sleepTime = calculateSleepTime();
System.out.println("Rate limit exceeded. Sleeping for " + sleepTime + "ms");
Thread.sleep(sleepTime);
resetWindow();
}
makeApiRequest();
requestCount.incrementAndGet();
}
}
private static boolean isRateLimitExceeded() {
long currentTime = System.currentTimeMillis();
long window = currentTime - windowStart.get();
if (window >= 1000) { // 1 second window
return false;
}
return requestCount.get() >= MAX_REQUESTS_PER_SECOND;
}
private static long calculateSleepTime() {
return 1000 - (System.currentTimeMillis() - windowStart.get());
}
private static void resetWindow() {
windowStart.set(System.currentTimeMillis());
requestCount.set(0);
}
private static void makeApiRequest() {
System.out.println("Making API request at " + System.currentTimeMillis());
}
}
Polling is a common technique in software development where a thread repeatedly checks a condition until it becomes true. It’s often used in scenarios where the state of a resource is not immediately available, and the thread needs to wait for it to be ready. For example, polling is commonly used in web applications to check for new messages or updates in real-time.
Thread.sleep()
can be combined with exponential backoff to implement a robust polling mechanism. By increasing the delay between each attempt, we can avoid overwhelming the system with frequent requests and ensure that the thread waits for the resource to be available before making another attempt. This approach is particularly useful for polling resources that are not always available, such as external APIs or databases.
package com.journaldev.threads;
public class PollingWithBackoff {
private static final int MAX_RETRIES = 5;
private static final long INITIAL_DELAY = 1000;
private static final double BACKOFF_MULTIPLIER = 2.0;
public static void main(String[] args) throws InterruptedException {
for (int attempt = 0; attempt < MAX_RETRIES; attempt++) {
try {
if (checkResourceAvailability()) {
System.out.println("Resource is now available!");
break;
}
} catch (Exception e) {
System.out.println("Attempt " + (attempt + 1) + " failed: " + e.getMessage());
}
if (attempt < MAX_RETRIES - 1) {
long delay = calculateBackoffDelay(attempt);
System.out.println("Waiting " + delay + "ms before next attempt");
Thread.sleep(delay);
}
}
}
private static boolean checkResourceAvailability() throws Exception {
// Simulate resource check
if (Math.random() < 0.3) { // 30% success rate
return true;
}
throw new Exception("Resource not available");
}
private static long calculateBackoffDelay(int attempt) {
return (long) (INITIAL_DELAY * Math.pow(BACKOFF_MULTIPLIER, attempt));
}
}
Method | Use Case | Lock Release | Precision | Interruption |
---|---|---|---|---|
Thread.sleep() |
Timed waiting | No | Low | Yes |
Object.wait() |
Waiting for notification | Yes | High | Yes |
LockSupport.parkNanos() |
Low-level parking | No | High | Yes |
ScheduledExecutorService |
Scheduled tasks | N/A | High | Yes |
ScheduledExecutorService
is a powerful tool for scheduling tasks with precise timing and flexibility. It’s particularly useful for implementing complex scheduling patterns, such as periodic tasks, delayed execution, and rate limiting.
The following example demonstrates how to use ScheduledExecutorService
to schedule a task to run after a specified delay and to run periodically at a fixed rate.
package com.journaldev.threads;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorExample {
public static void main(String[] args) throws InterruptedException {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
// Schedule a task to run after 2 seconds
scheduler.schedule(() -> {
System.out.println("Task executed after 2 seconds");
}, 2, TimeUnit.SECONDS);
// Schedule a task to run every 1 second
scheduler.scheduleAtFixedRate(() -> {
System.out.println("Periodic task executed at " + System.currentTimeMillis());
}, 0, 1, TimeUnit.SECONDS);
// Let it run for 10 seconds
Thread.sleep(10000);
scheduler.shutdown();
}
}
CompletableFuture
is a modern alternative to Thread.sleep()
that provides a more flexible and scalable way to handle asynchronous operations. It’s particularly useful for implementing complex scheduling patterns, such as periodic tasks, delayed execution, and rate limiting.
The following example demonstrates how to use CompletableFuture
to schedule a task to run after a specified delay and to run periodically at a fixed rate.
package com.journaldev.threads;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class CompletableFutureExample {
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
public static void main(String[] args) throws InterruptedException {
CompletableFuture<String> future = CompletableFuture
.supplyAsync(() -> "Initial task")
.thenApplyAsync(result -> {
try {
Thread.sleep(1000); // Simulate work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return result + " - processed";
})
.orTimeout(5, TimeUnit.SECONDS);
future.thenAccept(System.out::println);
Thread.sleep(2000);
scheduler.shutdown();
}
}
InterruptedException
is a checked exception thrown when another thread interrupts the sleeping thread. It’s crucial to handle this exception properly by either re-interrupting the thread (Thread.currentThread().interrupt()
) or handling the interruption gracefully. This allows the application to respond to shutdown signals and other interruption requests.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
// Handle interruption appropriately
}
Thread.sleep()
can be used for the following use cases:
While Thread.sleep()
is simple, it’s not ideal for high-precision timing or complex scheduling due to operating system scheduler limitations. For more precise control, consider these alternatives:
Use ScheduledExecutorService
for precise timing
ScheduledExecutorService
allows you to schedule tasks to run after a specified delay or at regular intervals. It’s more robust for managing timed tasks in an application.
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorServiceExample {
public static void main(String[] args) throws InterruptedException {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
System.out.println("Task scheduled at: " + System.currentTimeMillis());
// Schedule a task to run after 1 second
scheduler.schedule(() -> {
System.out.println("Task executed at: " + System.currentTimeMillis());
}, 1, TimeUnit.SECONDS);
// Keep the main thread alive long enough for the scheduled task to run
Thread.sleep(2000);
scheduler.shutdown();
}
}
Use LockSupport.parkNanos()
for high-precision delays
LockSupport.parkNanos()
is a low-level utility that can be used for very fine-grained control over thread parking. It’s often used in concurrency frameworks for high-performance scenarios where Thread.sleep()
's precision is insufficient.
import java.util.concurrent.locks.LockSupport;
public class LockSupportParkNanosExample {
public static void main(String[] args) {
long start = System.nanoTime();
long nanosToPark = 1_000_000_000L; // 1 second in nanoseconds
System.out.println("Parking for 1 second using LockSupport.parkNanos()");
LockSupport.parkNanos(nanosToPark); // Park the current thread
long end = System.nanoTime();
System.out.println("Unparked. Actual park time in ns = " + (end - start));
}
}
Thread.sleep()
can block the UI thread, causing the application to freeze. This is not recommended as it can lead to a poor user experience.
To avoid this, use a background thread to perform the sleep operation.
// BAD: Blocks UI thread
public void onClick() {
try {
Thread.sleep(5000); // UI freezes for 5 seconds
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// GOOD: Use background thread
public void onClick() {
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(5000);
// Update UI on main thread
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
}
Thread.sleep()
does not release the locks the current thread has acquired. This can lead to deadlocks if not properly managed.
To avoid this, release the lock before sleeping.
// BAD: Holds lock while sleeping
synchronized (lock) {
Thread.sleep(1000); // Other threads wait for lock
// Critical section
}
// GOOD: Release lock before sleeping
synchronized (lock) {
// Critical section
}
// Sleep outside synchronized block
Thread.sleep(1000);
Thread.sleep()
is not suitable for precise timing due to operating system scheduler limitations. For more precise control, consider using ScheduledExecutorService
.
To avoid this, use ScheduledExecutorService
for precise timing.
// BAD: Not suitable for precise timing
long start = System.currentTimeMillis();
Thread.sleep(1000);
long actual = System.currentTimeMillis() - start; // May not be exactly 1000ms
// GOOD: Use ScheduledExecutorService for precise timing
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.schedule(() -> {
// Precise timing
}, 1000, TimeUnit.MILLISECONDS);
System.nanoTime()
for high-precision measurementsThread.sleep()
do in Java?Thread.sleep()
pauses the execution of the current thread for a specified duration. The thread enters a TIMED_WAITING
state and doesn’t consume CPU resources during the sleep period. The actual sleep time may vary slightly due to system timer precision and thread scheduling.
InterruptedException
in Thread.sleep()
?InterruptedException
is a checked exception thrown when another thread interrupts the sleeping thread. It’s crucial to handle this exception properly by either re-interrupting the thread (Thread.currentThread().interrupt()
) or handling the interruption gracefully. This allows the application to respond to shutdown signals and other interruption requests.
Thread.sleep()
different from wait()
?The key differences between Thread.sleep()
and wait()
are summarized in the table below:
Feature | Thread.sleep() |
wait() |
---|---|---|
Lock Release | Does not release any monitor locks. | Releases the object’s monitor lock. |
Notification | Wakes up only after the specified time duration. | Can be woken up by notify() , notifyAll() , or after a timeout. |
Synchronization | Can be called anywhere. | Must be called within a synchronized block. |
Use Case | Used for simple timed delays. | Used for waiting for a specific condition to become true. |
Thread.sleep()
pause the whole program?No, Thread.sleep()
only pauses the current thread, not the entire program. Other threads continue to execute normally. However, if called on the main thread, it will pause the main thread’s execution, which might make the application appear unresponsive if it’s a single-threaded application.
Thread.sleep()
?Modern alternatives include:
Thread.sleep()
suitable for production code?Thread.sleep()
can be used in production code for specific use cases like rate limiting, polling with delays, and testing. However, consider alternatives like ScheduledExecutorService
for better resource management and precision. Always handle InterruptedException
properly and avoid using it in UI threads or critical sections where it might cause performance issues.
In this tutorial, you learned about Java’s Thread.sleep()
method, including its advanced mechanics, performance implications, AI applications, and modern alternatives. Understanding when and how to use Thread.sleep()
effectively is crucial for building robust, scalable Java applications.
The method remains valuable for specific use cases like rate limiting, polling, and testing, but modern Java development increasingly favors reactive programming patterns and more sophisticated concurrency utilities. By following best practices and understanding the trade-offs, you can leverage Thread.sleep()
effectively while building toward more scalable solutions.
To deepen your understanding of Java concurrency, explore related topics such as Multithreading in Java, how to use Thread.wait(), notify(), and notifyAll() for inter-thread communication, and the Thread.join() method for thread synchronization.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
I help Businesses scale with AI x SEO x (authentic) Content that revives traffic and keeps leads flowing | 3,000,000+ Average monthly readers on Medium | Sr Technical Writer @ DigitalOcean | Ex-Cloud Consultant @ AMEX | Ex-Site Reliability Engineer(DevOps)@Nutanix
Thanks for short and useful post on Thread.sleep() method. I think 2000 is meant, and not 200, here: “If you will run the above program, you will notice that the sleep time it prints is slightly greater than 200 and caused by …” in this post. Please amend this.
- Rishi Raj
hi sir just one question, once the thread is in sleep mode how can it be interrupted? You have mentioned this in 4 point as “Any other thread can interrupt the current thread in sleep, in that case InterruptedException is thrown.” thanks ashish
- Ashish
class TestCallRun extends Thread{ public void run(){ for(int i=1;i<5;i++){ try{Thread.sleep(5000);}catch(InterruptedException e){System.out.println(e);} System.out.println(i); } } public static void main(String args[]){ TestCallRun2 t1=new TestCallRun2(); TestCallRun2 t2=new TestCallRun2(); t1.start(); t2.run(); } } why the output of above program is same as when we call t1.start(); t2.start(). 1 1 2 2 3 3 4 4 but output is different when we call t1.run();t2.run(); 1 2 3 4 1 2 3 4 according to my understating output of t1.start(); t2.run() should be 1 --t1 thread 1 --t2 thread 2 --t2 thread 3 --t2 thread 4 --t2 thread 2 --t1 thread 3 --t1 thread 4 --t1 thread
- Nitin
Hi Pankaj, Your blog is very good and more informative. But its very good if you provide link to export to pdf. Thanks Maruthi
- maruthi
Hi Pankaj, I modified your code like below, and I can see same sleep time after every execution – long sleepTime = 999; System.out.println("Going to sleep for "+sleepTime); long start = 0L; try { start = System.currentTimeMillis(); Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start)); I guess, the difference is coming due to the startTime capture statement execution and the actual sleep statement, if you keep them one after the other, you won’t see the difference anymore. I understand that your explanation is correct too, because all thread execution depends on how OS allow them. Thanks
- Punit
Thread One = new Thread( ()-> {}); What is the difference between calling One.sleep() and Thread.sleep();
- James
long start = System.currentTimeMillis(); what does this line have in the code?
- FREDY ORLANDO MARCELO CASTIBLANCO
Suppose if a thread is kept in sleep and after completing sleep mode time the processor is running another thread. then will the current thread stops and executes thread that completed sleep mode or thread that is in sleep mode executed after current running process is terminated please explain it in detail I am new to java
- Pavan
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.