Exception found while using Thread.sleep() and wait() in Java

0 votes
unreported exception java.lang.InterruptedException; must be caught or declared to be thrown.

May 9, 2018 in Java by parth295
• 4,640 points
2,328 views

2 answers to this question.

0 votes

 From compiler errors through exception handling, threading and thread interruptions. But this will do what you want:

try {
    Thread.sleep(1500);                 //1500 milliseconds is one second.
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
answered May 9, 2018 by sharth
• 3,350 points
0 votes

A simpler way to wait is to use System.currentTimeMillis(), which returns the number of milliseconds since midnight on January 1, 1970 UTC. For example, to wait 5 seconds:

public static void main(String[] args) {
    //some code
    long original = System.currentTimeMillis();
    while (true) {
        if (System.currentTimeMillis - original >= 5000) {
            break;
        }
    }
    //more code after waiting
}
answered Aug 13, 2018 by samarth295
• 2,190 points

Related Questions In Java

0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
106 views
0 votes
2 answers

Give an example for encryption and decryption in AES using Java

getfullstring: Splashscreen.text >>> READ MORE

answered May 8 in Java by anonymous
2,013 views
0 votes
1 answer

How can we use wait and notify in Java?

You can refer to this: synchronized (someObject) { ...READ MORE

answered Jul 12, 2018 in Java by sophia
• 1,400 points
28 views
0 votes
1 answer

How can you catch an exception thrown by another thread in Java?

This can be done using Thread.UncaughtExceptionHandler. Here’s a simple ...READ MORE

answered Dec 3, 2018 in Java by Frankie
• 9,810 points
1,919 views
0 votes
1 answer

difference between wait() and sleep() in java

We can find a big difference between ...READ MORE

answered Apr 25, 2018 in Java by developer_1
• 3,300 points
67 views
0 votes
2 answers

Does Java thread.sleep() puts swing ui to sleep?

You are correct about the code putting ...READ MORE

answered Oct 24, 2018 in Java by Sushmita
• 6,880 points
512 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered Apr 30, 2018 in Java by sharth
• 3,350 points
113 views
0 votes
1 answer

Gracefully stopping a java thread

The preferable way will be to use a ...READ MORE

answered Aug 20, 2018 in Java by anto.trigg4
• 3,420 points
38 views
0 votes
2 answers

When to use LinkedList and ArrayList in Java?

ArrayList is what you want. LinkedList is almost always a ...READ MORE

answered Dec 11, 2018 in Java by Sushmita
• 6,880 points
124 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 30,500 points
166 views