rahul soni
rahul soni

Reputation: 35

synchronized methods executing simulaneously

As per synchronized keyword, if applied on to the method then it acquires the object lock and multiple methods with same instance would not be able to execute the methods concurrently.

But in the below example, the execution is happening concurrently. please have a look:-

public class ncuie extends Thread{

    int b;
    ncuie(int a){
        b=a;
    }
    
    public synchronized void abc() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            System.out.println("thread 1 "+i);
        }
    }
    
    public synchronized void pqr() throws InterruptedException {

        for (int i = 0; i < 10; i++) {
            System.out.println("thread 2 "+i);
        }
        
    }
    
    public void run() {
        
        try {
            if(b==5) {
            abc();
            }
            if(b==10) {
            pqr();
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public static void main(String[] args) {
        Thread t=new ncuie(5);
        Thread t1=new ncuie(10);
        t.start();
        t1.start();
    }
}

As per the output, sometimes thread 1 is executed and sometimes thread2. Ideally only, one thread should have gained the lock and completed it execution, then only the second should have started.

Output:-

thread 1 0
thread 1 1
thread 2 0
thread 2 1
thread 2 2
thread 1 2
thread 1 3
thread 1 4
thread 1 5
thread 1 6
thread 1 7
thread 1 8
thread 2 3
thread 2 4
thread 2 5
thread 2 6
thread 1 9
thread 2 7
thread 2 8
thread 2 9

Upvotes: 0

Views: 107

Answers (2)

Ralf Kleberhoff
Ralf Kleberhoff

Reputation: 7290

You typically synchronize on your instance if some methods change internal state in such a way that other methods operating on the same instance would get into trouble. Synchronizing on the current instance (this) is what you get by using the synchronized keyword on a non-static method, or by writing synchronized(this) {...}.

If your methods change state in such a way that even methods operating on a different instance of the class get affected, you can synchronize on the class object. You do this by writing synchronized(MyClass.class) {...}.

If you have static methods that need synchronization against other static methods of the same class, you can get the synchronization on the class object by using the synchronized keyword on the static method.

Be aware that instance-level synchronization and class-level synchronization are completely independent, meaning that, while a class-synchronized method runs, instance-synchronized methods are not blocked.

In your case, you synchronized on the instance, and you have two different instances, so they are not blocking one another.

Upvotes: 4

talex
talex

Reputation: 20455

synchronized acquire lock on this. You have two instances of your ncuie class, so it is perfectly fine to have them both locked simultaneously.

To solve it you need to share some instance between your thread. I suggest to create new Object and pass it into constrictor.

Upvotes: 2

Related Questions