java sleep in main method

java sleep in main method

Hi,

How to write Java program for sleeping in the main method?

I want Java program to have sleep in main method. Try to share me the code examples.

Thanks

View Answers

April 28, 2020 at 6:13 AM

Hi,

Yes it is possible to write such example. Here is the example code which sleeps for 500 milliseconds and then print the count:

package net.roseindia;

public class SleepMethodExample extends Thread {

    public void run() {
        for (int i = 0; i < 10; i++) {
            // Sleep for 500 milliseconds
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                System.out.println(e);
            }
            System.out.println(i);
        }
    }

    public static void main(String[] args) {
        SleepMethodExample obj = new SleepMethodExample();
        obj.start();
    }

}

We have extended the SleepMethodExample with Thread class to make it threaded application.

Then implemented the run() method. Inside the run() method we have used Thread.sleep(500); for sleeping the thread for 500 milliseconds.

Hope this helps you.

Thanks


April 28, 2020 at 6:17 AM

Hi,

How Thread Sleep Works?

In this example we have used the Thread.sleep() method, which actually interacts with the thread scheduler of the program and then put the current thread in wait state for the time specified in the parameter. The Thread.sleep() method takes parameter in milliseconds and it sleeps the thread for specified milliseconds.

Once the sleep time is over thread state is changed to runnable state and normal execution of program starts after that.

Thanks


April 28, 2020 at 8:27 AM

Hi,

Here is the screen shot of the program discussed here.

Thanks









Related Tutorials/Questions & Answers:
java sleep in main method
java sleep in main method  Hi, How to write Java program for sleeping in the main method? I want Java program to have sleep in main method. Try to share me the code examples. Thanks
java sleep in main method
java sleep in main method  Hi, How to write Java program for sleeping in the main method? I want Java program to have sleep in main method. Try to share me the code examples. Thanks
Advertisements
sleep method in thread java program
sleep method in thread java program  How can we use sleep method... example ,we have used sleep method. we are passing some interval to the sleep... Description:- In this thread example ,we have used sleep method. we are passing
Java Thread : sleep() method
Java Thread : sleep() method In this section we are going to describe sleep() method with example in java thread. sleep() method : Suppose you want to stop your thread for a specific time period, you can use sleep() method
example of sleep and wait method
example of sleep and wait method  write a program to use the sleep and wait method of thread class
Is main method compulsory in Java?
Is main method compulsory in Java?  Hi, Can we have a executable class in Java without a main method? I am going to write simple Java program and there is main method. Is main method compulsory in Java? Thanks   Hi
When is java main method called?
When is java main method called?  When is java main method called? Please explain the main method in Java with the help of code.   In a java class, main(..) method is the first method called by java environment when
main method
main method  Why is the java main method static
Java Sleep Thread
Java Thread sleep() is a static method. It sleeps the thread for the given... or GUI programming for animation Java Sleep Thread Example public class... " + Thread.currentThread()); } } } public static void main(String[] args
main method
main method  What is the argument type of a program's main() method
main method
in paranthesis denote?   Hi Friend, public-It indicates that the main() method can be called by any object. static-It indicates that the main() method is a class method. void- It indicates that the main() method has no return value
main method
; hello No we can not make multiple main with in the same class program fails to compile. Compiler gives error that the main method is already defined...main method  hello, Can I make multiple main methods in the same
main() method
main() method  Can we define two main() methods having same parameter but with different return type in a program
Main method overloading
Main method overloading  Can we overload main()method
What is the arguement of main method?
What is the arguement of main method?  hi, What is the arguement of main method? thanks,   Hi, For main() method accepts only an array of String object as arguement. For moe details on http://www.roseindia.net/java
Can a main method be overloaded?
Can a main method be overloaded?   Hi, Can a main method be overloaded? thanks,   Hi, Yes, In Java program we could having multiple number of main() methods with different Signature and implementation in the class
Should a main method be compulsorily declared in all java classes?
Should a main method be compulsorily declared in all java classes?  hi, Should a main method be compulsorily declared in all java classes? thanks... in all java classes. Where as the main method we should be defined only if the source
Accessing non-static members through the main method in Java.
Accessing non-static members through the main method in Java.  As an oop rule, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method
Why we should use string args[] in main method in java?
Why we should use string args[] in main method in java?  we use only string in the main method not any other one.. specify the reason... and tell me... line from the command prompt   In the main method,the String array args
PHP Sleep Wakeup Method
__sleep or not. If so, then the function execute that method prior to any other... any resources that the object may have. PHP Sleep and Wakeup Method Example...The __sleep and __wakeup Function in PHP The __sleep function in PHP is useful
What is the return type of the main method?
What is the return type of the main method?  hi, What is the return type of the main method? thanks   Hi, In the java programming the Main() method doesn't return anything hence declared void. In Java, you need
What if the main() method is declared as private?
What if the main() method is declared as private?  Hi, What if the main() method is declared as private? Thanks   Hi, I have found some reference site for java programming coding for Main() method is declared
Can a main method be declared final?
be override in a subclass. for more about main method be declared final in Java...Can a main method be declared final?   Hi, Can a main method be declared final? Thanks   Hi, Yes we can. The final method can
I want only one method goes to sleep but both method goes to sleep together
I want only one method goes to sleep but both method goes to sleep... want like a sleep() or any other method should be invoked between User's turn and Computer's turn. But when i call Sleep() before cpuPlay() method or after
I want only one method goes to sleep but both method goes to sleep together
I want only one method goes to sleep but both method goes to sleep... want like a sleep() or any other method should be invoked between User's turn and Computer's turn. But when i call Sleep() before cpuPlay() method or after
Why is the main method declared static?
Why is the main method declared static?  Hi, Why is the main method declared static? thanks   Hi, When we declare main() method in any Java class always has the same signature, and we declare public static void main
JAVA what is different between static block and public static void main(String a[]) method
JAVA what is different between static block and public static void main(String... void main(String a[]) method,we execute method without main method(by static... are meant to be run once the corresponding class is loaded. The main() method
What is the first argument of the String array in main() method?
What is the first argument of the String array in main() method?  Hi, What is the first argument of the String array in main() method? Thanks... of the String array in main() method
Till Slip Program Error Free - No Main Method
Till Slip Program Error Free - No Main Method  Hi there i am a java... that the program recquires a main() method in order to be runned - here is the following code...() { initComponents(); } /** * This method is called from within
Till Slip Program Error Free - No Main Method
Till Slip Program Error Free - No Main Method  Hi there i am a java... that the program recquires a main() method in order to be runned - here is the following code...() { initComponents(); } /** * This method is called from within
Till Slip Program Error Free - No Main Method
Till Slip Program Error Free - No Main Method  Hi there i am a java... that the program recquires a main() method in order to be runned - here is the following code...() { initComponents(); } /** * This method is called from within
Till Slip Program Error Free - No Main Method
Till Slip Program Error Free - No Main Method  Hi there i am a java... that the program recquires a main() method in order to be runned - here is the following code...() { initComponents(); } /** * This method is called from within
Till Slip Program Error Free - No Main Method
Till Slip Program Error Free - No Main Method  Hi there i am a java... that the program recquires a main() method in order to be runned - here is the following code...() { initComponents(); } /** * This method is called from within
Till Slip Program Error Free - No Main Method
Till Slip Program Error Free - No Main Method  Hi there i am a java... that the program recquires a main() method in order to be runned - here is the following code...() { initComponents(); } /** * This method is called from within
__sleep and __wakeup
__sleep and __wakeup  What?s the special meaning of _sleep and _wakeup
main() syntax - Java Beginners
] [import statements] [class declaration] [main() method] For Example: package...main() syntax  Hi, I would like to know the complete structure of the main() in jdk1.5 with sample program. Regards, UsmanAli Shaik,  
how to open one Jframe from main method call
how to open one Jframe from main method call  I have downloaded... the main() method, when i run this Engine.java class game starts running...(containing main() method) Snake.java GameBoard.java PlayGame.java
yield and sleep
yield and sleep  hello, What is the difference between yield() and sleep
pack() vs. setSize() Method in Java
or the setSize() method of Java Swing.ADS_TO_REPLACE_1 The main difference between... pack() vs. setSize() Method in Java   ... illustrates you about the main difference between the pack() method and the setSize
Main function.. - Java Beginners
Main function..  Hi Friend.. public static void main(String args[]) What does it mean...public - static - void - main -(String args[]) Can u plz...   Hi friend, public: The method can be accessed outside the class
What is Public static void main
[]) //this is most classic signature of main method. Remember the varargs version of java... method in java: Public:  main method is declared public because it is visible... there would not be any certain way for JVM to find main method in Java. ADS
main func - Java Beginners
main func  why do we pass array of strings in main function of java?  Hi Friend, It is used to signify that the user may opt to enter parameters to the java program at command line. Thanks
problem with main - Java Beginners
, This is an applet code and applet doesn't have main method therefore compile it and run... a problem. when i compile it appears this message: java.lang.NoSuchMethodError: main Exception in thread "main" . Should i put a main in another file? And how can
java method
java method  can we declare a method in java like this static { System.loadLibrary("nativelib"); } i have seen this in a java learning E book. i don't understand the static { } plz help me. what kind of method
java main program
java main program  how to write a main program for the following code public class JaroWinkler { private String compOne; private String compTwo; private String theMatchA = ""; private String theMatchB
ModuleNotFoundError: No module named 'sleep'
ModuleNotFoundError: No module named 'sleep'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'sleep' How to remove the ModuleNotFoundError: No module named 'sleep'
Main topics in java for IT corporate working?
Main topics in java for IT corporate working?  What are the main and most important topics in java should be mastered that are usually incorporated in IT corporate world
pass method reference in java
pass method reference in java  How to pass method reference in Java
getch() method in java
getch() method in java  does someone know how can i obtain the getch() function in java?   Hi Friend, In java, you can use the method...*; class Hello{ public static void main(String[] args) { Scanner input
main function defnition in class - Java Beginners
of a main function   Hi friend, The main method is similar to the main... and will subsequently invoke all the other methods required by your program. The main method... because we can use that method(main) not only in the current directory

Ads