Skip to content
← Back to Community
Is there anyway to delay code in java on here?
Profile icon
MisledWater79

I’m trying to make something in java and I don’t know how to delay it.

Answered by PDales [earned 5 cycles]
View Answer
Voters
Profile icon
MisledWater79
Comments
hotnewtop
Profile icon
PDales

long elapsedTime, timeStart;
long milliseconds = 5000;
timeStart = System.currentTimeMillis(); //new Date().getTime();
while (true)
{
elapsedTime = System.currentTimeMillis() - timeStart;
if (elapsedTime > milliseconds)
{
break;
}
}

Profile icon
MisledWater79

@PDales
How did you find this post lol, it was over a year ago i posted this

Profile icon
Coder100

You can use sleep the thread, which delays code.

https://www.tutorialspoint.com/java/lang/thread_sleep_millis.htm

it's in milliseconds, however.

Profile icon
MisledWater79

@Coder100
I can’t get it to work.. this is the code I have
class DataBase implements Runnable {
Thread t;
static void main(String User){
System.out.println("Welcome " + User);
while (User == "Kris79"){
Thread.sleep(5000);
System.out.println("0 0");
}
}
}
It keeps saying that the class part has a error

Profile icon
RhinoRunner

you can use Thread.sleep().

Thread.sleep(1000) //pauses for 1 second