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/']
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());
    }

}

```

}
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);
    }
}

```