当前位置: 动力学知识库 > 问答 > 编程问答 >

multithreading - Thread 1 is executing in java synchronized method 1, can Thread 2 then execute in java synchronised method 2?

问题描述:

Was wondering if someone could help clear this up for me. (Student)

Say we have two threads, "Thread1" & "Thread2". If Thread1 is executing in method 1 can Thread2 then execute in method2?

void method1() {

synchronized (this) {

}

}

void method2() {

synchronized (this) {

}

}

I'm either thinking yes, Thread2 can enter as "this" is just the instance of that method or no because "this" is the instance of that class and Thread1 holds onto it.

网友答案:

There isn't a monitor associated with a specific method - there's a monitor associated with an object. So if you're trying to synchronize on the same object in both methods, the second thread will block until the first thread releases the monitor.

(Personally I don't like synchronizing on this anyway - I synchronize on a reference to an object that only my class has access to. But that's a different matter.)

网友答案:

Everybody jumped on an obvious answer here. It is a correct answer, but it is not the only correct answer.

The rule is, two threads can not both synchronize on the same object at the same time. But does this refer to the same object in both method calls? We can't tell because you have not shown us the calls. Consider this example:

class Demo {

    synchronized void method1() {
        ...do something...
    }

    synchronized void method2() {
        ...do something else...
    }

    static void caseA() {
        final Demo demo = new Demo();
        new Thread(new Runnable(){
            @Override
            public void run() {
                demo.method1();
            }
        }).start();
        new Thread(new Runnable(){
            @Override
            public void run() {
                demo.method2();
            }
        }).start();
    }

    static void caseB() {
        final Demo demo1 = new Demo();
        final Demo demo2 = new Demo();
        new Thread(new Runnable(){
            @Override
            public void run() {
                demo1.method1();
            }
        }).start();
        new Thread(new Runnable(){
            @Override
            public void run() {
                demo2.method2();
            }
        }).start();
    }
}

In caseA(), the calls to demo.method1() and demo.method2() can not overlap because both calls synchronize on the same object, but in caseB() the two calls are synchronized on two different instances of the Demo class. In caseB() the method1() call and the method2() call can overlap.

网友答案:

You are using this for synchronization, which is an instance of an object. Synchronization always works on some particular instance of object and only one thread at any given time can acquire lock of (synchronize on) a single instance used for synchronization and.

Both threads can access the methods only if they use separate instances of the object containing those methods.

网友答案:

The second thread will block until the monitor is released by the first as synchronization is done on the same object for both methods.

网友答案:

You'd better synchronized on the static object you want to lock. Here, your method 2 will be executed by thread 2, but the content of the synchronized block in method 2 will not be executed unless thread 1 has unlock this. Since your synchronization is on a block, and not on a method, the method is not synchronized, but the block is.

If you have a static member on which you want work in the synchronized, use it like:

private static List<Object> myList;



 void method1() {
    synchronized (myList) {
        // do something on myList
    }
}

void method2() {
    synchronized (myList) {
       // do something  on myList
    }
}
网友答案:

No Thread2 will have to wait until Thread1 is finished executing the code in synchronised block. This is because you are using the same object (this) to lock. If you did something like this:

Object lock1 = new Object();
Object lock2 = new Object();

void method1() {
    synchronized (lock1) { }
}


void method2() {
    synchronized (lock2) { }
}

Then you would be able to have Thread1 execute method1 and Thread2 execute method2.

分享给朋友:
您可能感兴趣的文章:
随机阅读: