Eduardo
Eduardo

Reputation: 2280

Java Threads Object Lock

According to my understanding, every object has a lock that can be hold by the current thread.

So, based on this example:

public class T1 {
    private final Object o = new Object();

    public  void m1() {
        synchronized(o) {
            //...wait some time
        }
    }
}

If I create something like:

    T1 subject1 = new T1();
    T1 subject2 = new T1();
    Runnable r1 = new Runnable() {
        @Override
        public void run() {
            subject1.m1();
            subject2.m1();  
        }
    };

Thread t1 = new Thread(r1);
t1.start();

There will be two locks, subject1.o and subject2.o (I tested they are different instances), which means both methods will run independently and at the same time, well, in my example one is executed and then, after it releases the lock, the next runs although subject1 and subject2 are different instances with different locks.

Why?

Upvotes: 1

Views: 100

Answers (3)

Solomon Slow
Solomon Slow

Reputation: 27115

There will be two locks, subject1.o and subject2.o ..., which means both methods will run independently and at the same time.

No. The fact that there are two locks does not mean that two calls to the m1 function will run independently and at the same time. It only means that two calls can run at the same time. But that can only happen if each of those two calls happens in a different thread.

What other answers here have already tried to tell you is, your code doesn't make the two calls from two different threads. It makes both calls from the same thread. You wrote this run() method:

public void run() {
    subject1.m1();
    subject2.m1();  
}

There's nothing special about a run() method. YOU wrote it, and it will work exactly like any other method that you write: The one that you wrote will first call subject1.m1(), and then when that returns, it will call subject2.m1().

If you want to make those calls in parallel, then do what @Palamino or @snr showed you.

Upvotes: 1

There will be two locks, subject1.o and subject2.o (I tested they are different instances), which means both methods will run independently and at the same time

You say both methods will run independently though you have only one enclosing method to run both threads. Instead, both methods have to be within different Runnables to be executed concurrently.

T1 subject1 = new T1();
T1 subject2 = new T1();
Runnable r1 = subject1::m1;
Runnable r2 = subject2::m1;
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();

Upvotes: 1

Palamino
Palamino

Reputation: 791

In your example, you are running the two method sequentially in the same thread. To execute them in parallel, you must run them in separate threads.

T1 subject1 = new T1();
T1 subject2 = new T1();
Runnable r1 = new Runnable() {
    @Override
    public void run() {
        subject2.m1();  
    }
};
Runnable r2 = new Runnable() {
    @Override
    public void run() {
        subject2.m1();  
    }
};

Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();

Upvotes: 2

Related Questions