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)
Why you are getting different output?

Answer:
synchronized requires an object on which lock is attained before entering the synchronized block or method.

In this case lock is being attained on the objects on which the method is executed hence thread 1 is locking on object th1, thread 2 is locking on th2 and so on.

therefore, all the threads are locking onto different objects and are allowed to run simultaneously.

How to get consistent output?

Answer

option 1: you can use join to join the threads to main thread
public class MultiBasic1 {

```
public static void main(String[] args) throws InterruptedException {
    ChildThread th1=new ChildThread();
    ChildThread th2= new ChildThread();
    ChildThread th3= new ChildThread();
    ChildThread th4= new ChildThread();

    th1.setName("A");
    th2.setName("B");
    th3.setName("C");
    th4.setName("D");

    th1.start();
    th1.join();
    th2.start();
    th2.join();
    th3.start();
    th3.join();
    th4.start();
    th4.join();
}

```

}

class ChildThread extends Thread{

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

```

}

option 2: provide shared object for synchronized block to synchronize the threads

public class MultiBasic1 {

```
public static void main(String[] args) throws InterruptedException {
    ChildThread th1=new ChildThread();
    ChildThread th2= new ChildThread();
    ChildThread th3= new ChildThread();
    ChildThread th4= new ChildThread();

    th1.setName("A");
    th2.setName("B");
    th3.setName("C");
    th4.setName("D");

    th1.start();
    th2.start();
    th3.start();
    th4.start();
}

```

}

class ChildThread extends Thread{

```
private static final Object lock = new Object();
public void run(){

    synchronized (lock) {
        for(int i=1; i<=5; i++)
            System.out.println(i + " " + getName());
    }

}

```

}