#297 Thread Synchronization for Robot Steps
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.
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
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor