N
N
Nodir Malikov2021-02-26 13:48:41
Java
Nodir Malikov, 2021-02-26 13:48:41

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

4 answer(s)
S
Sergey Gornostaev, 2021-02-26
@sergey-gornostaev

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.

R
Romses Panagiotis, 2021-02-28
@romesses

Perhaps it was meant to use event programming and implement this

state machines
traffic_light.jpg

Instead of Sleep use ScheduledThreadPoolExecutor.

G
gd1xza, 2021-02-26
@gd1xza

pseudo code

start_sleep_time = microtime(now)
while start_sleep_time < microtime(now){}

B
BorLaze, 2021-02-26
@BorLaze

you can't
write your own sleep():

sleep(int millis) {
  старт = текущее время системы в миллисекундах
  пока (текущее время < старт + millis) 
    ;
}

use our function where necessary

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question