Q:

How to create thread to control another?

I try to make simple multithreaded robot. He got Head as thread  and two legs, one leg - one thread. Everything works well, but how to create control thread, to control each legs?
my code is :
public class Controller implements CommandInterface{
    private final Object monitor = new Object();
    private int numOfSteps = 10;

class Legs implements Runnable {
    private final String whichLeg;

    Legs(String whichLeg) {
        this.whichLeg = whichLeg;
    }

    @Override
    public void run() {
        for(int i = 0; i < numOfSteps;i++) {
            synchronized (monitor) {
                step();
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                monitor.notify();
                try {
                    monitor.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void step() {
        System.out.println(whichLeg);
    }
}

    Legs left = new Legs("left feet");
    Legs right = new Legs("right feet");

    @Override
    public void execute() {
        new Thread(left).start();
        new Thread(right).start();

    }

}

To start robot i use this class:
public class ClientInterface {
public static void main(String [] args) {
        new Controller().execute();
    }
}

I know that i should use join() for waiting another thread. 
I want see result like this : 
Init head
 head sends make 1 step
 left feet
 head sends make 2 step
 right feet
 and so on...
i tried to create head thread at main method and call join() but it's wait for current thread, but i need to wait legs.i tried to create head thread like Thread head = new Thread(new Runnable{ tried here to run execute method }); but it's all didn't work.

A:

There are lots of possible options. Here is one of them:
Each leg becomes a monitor itself. When the Head thread is started it begins to notify/wait the legs. So on the first iteration the head will notify/wait the first leg, on the second iteration it will notify/wait the second leg, and so on until the end. Legs are just looping forever waiting to be notified by the Head thread. Their job is to receive the notification, print the appropriate message and send notification to the Head.
Here is the sketch:
interface CommandInterface {
    void execute();
}

class Controller implements CommandInterface {

    private static final int NUM_OF_STEPS = 10;

    private final Legs[] legs = {
        new Legs("left feet"),
        new Legs("right feet")
    };

    @Override
    public void execute() {
        Executors.newSingleThreadExecutor().execute(() -> {
            System.out.println("Init head");

            for (Legs leg : legs) {
                leg.start();
            }

            for (int i = 0; i < NUM_OF_STEPS; i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);

                    int currentLegIndex = i % legs.length;
                    synchronized (legs[currentLegIndex]) {
                        System.out.println("head sends make " + (i + 1) + " step");
                        legs[currentLegIndex].notify();
                        legs[currentLegIndex].wait();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException("Interrupted!", e);
                }
            }
        });

    }

    class Legs extends Thread {
        private final String whichLeg;

        Legs(String whichLeg) {
            this.whichLeg = whichLeg;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    synchronized (this) {
                        this.wait();
                        step();
                        this.notify();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException("Interrupted!", e);
                }
            }
        }

        private void step() {
            System.out.println(whichLeg);
        }
    }
}

class ClientInterface {
    public static void main(String [] args) {
        new Controller().execute();
    }
}

You might also consider creating a shared CountDownLatch. I would certanly recommend reading its javadoc. I think you'll understand the idea and create a more elegant solution by yourself ;)

