#297 Thread Synchronization for Robot Steps

Companies:
Sberbank
Themes:
JavaConcurrencyPractice

According to the exercise, the robot must walk with both legs strictly in turn (Left, Right, Left, Right, ...), starting with the left leg. Each leg is controlled by its own thread. At least 10 steps must be performed.

Java
public class Robot {

    public static void main(String[] args) throws InterruptedException {
        Thread leftLeg = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("Left");
            }
        });

        Thread rightLeg = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("Right");
            }
        });
        leftLeg.start();
        rightLeg.start();

        leftLeg.join();
        rightLeg.join();
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in