Everytime I get different output when I run this program.
Is there any way to get consistent output with in run method only?

```
public class MultiBasic1 {

    public static void main(String[] args) {
        ChildThread th1=new ChildThread();
        ChildThread th2= new ChildThread();
        ChildThread th3= new ChildThread();
        ChildThread th4= new ChildThread();
        th1.start();
        th2.start();
        th3.start();
        th4.start();
    }
}

class ChildThread extends Thread{

    synchronized public void run(){
        for(int i=1; i<=5; i++)
            System.out.println(i);
    }
}

```
['https://Stackoverflow.com/questions/41843490', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']
Because you're creating a new instance of `ChildThread`, each thread is allowed to access the `run` method without restriction, as there is only one thread actually accessing the method.

To demonstrate how `synchronized` works, you'd need to have some kind shared resource, such as a `Object` which each thread could interact with

```
public class Test {

    public static void main(String[] args) {
        Action action = new Action();
        Thread t1 = new Thread(new Runner(action), "1");
        Thread t2 = new Thread(new Runner(action), "2");
        Thread t3 = new Thread(new Runner(action), "3");
        Thread t4 = new Thread(new Runner(action), "4");
        Thread t5 = new Thread(new Runner(action), "5");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }

    public static class Runner implements Runnable {
        private Action action;

        public Runner(Action action) {
            this.action = action;
        }

        @Override
        public void run() {
            action.makeItSo();
        }

    }

    public static class Action {
        public synchronized void makeItSo() {
            for (int index = 0; index < 10; index++) {
                System.out.println(Thread.currentThread().getName() + " - " + index);
            }
        }
    }

}

```

The above example demonstrates that only one thread can run within the `makeItSo` method, but also, that there is no guarantee over which thread will be allowed to run (ie the order)
Here there is even no need of synchronization since you are using different objects for calling the run method.

Synchronization works when multiples threads of one object is in picture.

Above example will be more meaningful if you do something like below.

```
public class MultiBasic1 {

    public static void main(String[] args) {
        ChildThread th1=new ChildThread();
        Thread t1 = new Thread(th1);
        Thread t2 = new Thread(th1);
        Thread t3 = new Thread(th1);
        Thread t4 = new Thread(th1);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

class ChildThread extends Thread{

    synchronized public void run(){
        for(int i=1; i<=5; i++)
            System.out.println(i);
    }
}

```