如果我在同一个类上同步了两个方法,它们能同时在同一个对象上运行吗?例如:

class A {
    public synchronized void methodA() {
        //method A
    }

    public synchronized void methodB() {
        // method B
    }
}

我知道我不能在两个不同的线程中对同一个对象运行methodA()两次。在methodB()中也是如此。

但我可以运行methodB()在不同的线程,而methodA()仍在运行?(同一对象)


当前回答

不容易理解的同步的关键思想是,它只有在对同一个对象实例调用方法时才会起作用——这已经在回答和评论中突出显示了——

下面的示例程序是清楚地查明相同的-

public class Test {

public synchronized void methodA(String currentObjectName) throws InterruptedException {
    System.out.println(Thread.currentThread().getName() + "->" +currentObjectName + "->methodA in");
    Thread.sleep(1000);
    System.out.println(Thread.currentThread().getName() + "->" +currentObjectName + "->methodA out");
}

public synchronized void methodB(String currentObjectName)  throws InterruptedException {
    System.out.println(Thread.currentThread().getName() + "->" +currentObjectName + "->methodB in");
    Thread.sleep(1000);
    System.out.println(Thread.currentThread().getName() + "->" +currentObjectName + "->methodB out");
}

public static void main(String[] args){
    Test object1 = new Test();
    Test object2 = new Test();
    //passing object instances to the runnable to make calls later
    TestRunner runner = new TestRunner(object1,object2);
    // you need to start atleast two threads to properly see the behaviour
    Thread thread1 = new Thread(runner);
    thread1.start();
    Thread thread2 = new Thread(runner);
    thread2.start();
}
}

class TestRunner implements Runnable {
Test object1;
Test object2;

public TestRunner(Test h1,Test h2) {
    this.object1 = h1;
    this.object2 = h2;
}

@Override
public void run() {
    synchronizedEffectiveAsMethodsCalledOnSameObject(object1);
    //noEffectOfSynchronizedAsMethodsCalledOnDifferentObjects(object1,object2);
}

// this method calls the method A and B with same object instance object1 hence simultaneous NOT possible
private void synchronizedEffectiveAsMethodsCalledOnSameObject(Test object1) {
    try {
        object1.methodA("object1");
        object1.methodB("object1");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

// this method calls the method A and B with different object instances object1 and object2 hence simultaneous IS possible
private void noEffectOfSynchronizedAsMethodsCalledOnDifferentObjects(Test object1,Test object2) {
    try {
        object1.methodA("object1");
        object2.methodB("object2");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}

请注意,如果在不同的对象实例上调用方法,则输出中允许同时访问的方式是不同的。

输出带有noEffectOfSynchronizedAsMethodsCalledOnDifferentObjects()注释-输出是in order methodA in > methodA Out ..methodB in > methodB Out

和输出synchronizedEffectiveAsMethodsCalledOnSameObject()注释- 输出显示在突出显示的部分-中,Thread1和Thread0同时访问方法da

增加线程数将使其更加明显。

其他回答

不容易理解的同步的关键思想是,它只有在对同一个对象实例调用方法时才会起作用——这已经在回答和评论中突出显示了——

下面的示例程序是清楚地查明相同的-

public class Test {

public synchronized void methodA(String currentObjectName) throws InterruptedException {
    System.out.println(Thread.currentThread().getName() + "->" +currentObjectName + "->methodA in");
    Thread.sleep(1000);
    System.out.println(Thread.currentThread().getName() + "->" +currentObjectName + "->methodA out");
}

public synchronized void methodB(String currentObjectName)  throws InterruptedException {
    System.out.println(Thread.currentThread().getName() + "->" +currentObjectName + "->methodB in");
    Thread.sleep(1000);
    System.out.println(Thread.currentThread().getName() + "->" +currentObjectName + "->methodB out");
}

public static void main(String[] args){
    Test object1 = new Test();
    Test object2 = new Test();
    //passing object instances to the runnable to make calls later
    TestRunner runner = new TestRunner(object1,object2);
    // you need to start atleast two threads to properly see the behaviour
    Thread thread1 = new Thread(runner);
    thread1.start();
    Thread thread2 = new Thread(runner);
    thread2.start();
}
}

class TestRunner implements Runnable {
Test object1;
Test object2;

public TestRunner(Test h1,Test h2) {
    this.object1 = h1;
    this.object2 = h2;
}

@Override
public void run() {
    synchronizedEffectiveAsMethodsCalledOnSameObject(object1);
    //noEffectOfSynchronizedAsMethodsCalledOnDifferentObjects(object1,object2);
}

// this method calls the method A and B with same object instance object1 hence simultaneous NOT possible
private void synchronizedEffectiveAsMethodsCalledOnSameObject(Test object1) {
    try {
        object1.methodA("object1");
        object1.methodB("object1");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

// this method calls the method A and B with different object instances object1 and object2 hence simultaneous IS possible
private void noEffectOfSynchronizedAsMethodsCalledOnDifferentObjects(Test object1,Test object2) {
    try {
        object1.methodA("object1");
        object2.methodB("object2");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}

请注意,如果在不同的对象实例上调用方法,则输出中允许同时访问的方式是不同的。

输出带有noEffectOfSynchronizedAsMethodsCalledOnDifferentObjects()注释-输出是in order methodA in > methodA Out ..methodB in > methodB Out

和输出synchronizedEffectiveAsMethodsCalledOnSameObject()注释- 输出显示在突出显示的部分-中,Thread1和Thread0同时访问方法da

增加线程数将使其更加明显。

Java线程在进入实例同步Java方法时获得一个对象级锁,在进入静态同步Java方法时获得一个类级锁。

在您的示例中,方法(实例)属于同一个类。因此,当一个线程进入java synchronized方法或块时,它会获得一个锁(方法被调用的对象)。因此,在第一个方法完成并释放lock(on object)之前,不能在同一对象上同时调用其他方法。

这两种方法都锁定同一个监视器。因此,你不能同时从不同的线程在同一个对象上执行它们(两个方法中的一个会阻塞,直到另一个方法完成)。

清楚地说,静态同步和非静态同步方法都可能同时或并发地运行,因为一个有对象级锁,另一个有类级锁。

你同步的是对象而不是类。所以它们不能同时在同一物体上运行