0% found this document useful (0 votes)
7 views2 pages

5 Sleep Method in Java

The sleep() method in Java's Thread class allows a thread to pause for a specified duration. It has two overloaded versions that can take milliseconds and nanoseconds as parameters. The document includes an example demonstrating how to implement the sleep() method in a multi-threaded environment.

Uploaded by

Anima
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)
7 views2 pages

5 Sleep Method in Java

The sleep() method in Java's Thread class allows a thread to pause for a specified duration. It has two overloaded versions that can take milliseconds and nanoseconds as parameters. The document includes an example demonstrating how to implement the sleep() method in a multi-threaded environment.

Uploaded by

Anima
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
You are on page 1/ 2

9/23/2015 Sleep Method in Java ­ javatpoint

Content Menu ▼

Sleep method in java


The sleep() method of Thread class is used to sleep a thread for the
specified amount of time.

Syntax of sleep() method in java


The Thread class provides two methods for sleeping a thread:

public static void sleep(long miliseconds)throws


InterruptedException

public static void sleep(long miliseconds, int nanos)throws


InterruptedException

Example of sleep method in java

. class TestSleepMethod1 extends Thread{


. public void run(){
. for(int i=1;i<5;i++){
. try{Thread.sleep(500);}catch(InterruptedException e)
{System.out.println(e);}
. System.out.println(i);
. }
. }
. public static void main(String args[]){
. TestSleepMethod1 t1=new TestSleepMethod1();
. TestSleepMethod1 t2=new TestSleepMethod1();
.
. t1.start();
. t2.start();
. }
. }

Output:

1
1
2

http://www.javatpoint.com/sleep()­method 1/2
9/23/2015 Sleep Method in Java ­ javatpoint

2
3
3
4
4

As you know well that at a time only one thread is executed. If you
sleep a thread for the specified time,the thread shedular picks up
another thread and so on.

← prev next →

http://www.javatpoint.com/sleep()­method 2/2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy