Answer the question
In order to leave comments, you need to log in
How to pause code without sleep() in JAVA?
The fact is that I am doing such a task and it is forbidden to use the sleep method from Threading.
Create an Enum that represents the colors of the traffic lights. Write a program so that the traffic light does not stop for 3 minutes. The message says "Stop" for 4 seconds when the red light is on, "Wait" for 3 seconds when the yellow light is on, and "Let's go" for 5 seconds when the green light is on.
Note. The sleep() method of the Thread class cannot be used.
Answer the question
In order to leave comments, you need to log in
The context is unknown, so it is not entirely clear what the one who gave the task expects from you. Maybe it's worth using ScheduledExecutorService.
Perhaps it was meant to use event programming and implement this
ScheduledThreadPoolExecutor
.
pseudo code
start_sleep_time = microtime(now)
while start_sleep_time < microtime(now){}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question