如果我在同一个类中有2个同步方法,但是每个方法访问不同的变量,2个线程可以同时访问这2个方法吗?锁是发生在对象上,还是与同步方法中的变量一样特定?

例子:

class X {

    private int a;
    private int b;

    public synchronized void addA(){
        a++;
    }

    public synchronized void addB(){
        b++;
    }

}

两个线程可以同时访问类X的同一个实例,执行x.a da()和x.a bbb()吗?


当前回答

关于同步方法的“Java™教程”:

首先,对同一对象的同步方法的两次调用不可能交织。当一个线程正在为一个对象执行同步方法时,所有为同一对象调用同步方法的其他线程将暂停执行,直到第一个线程处理完该对象。

关于同步块的“Java™教程”:

Synchronized statements are also useful for improving concurrency with fine-grained synchronization. Suppose, for example, class MsLunch has two instance fields, c1 and c2, that are never used together. All updates of these fields must be synchronized, but there's no reason to prevent an update of c1 from being interleaved with an update of c2 — and doing so reduces concurrency by creating unnecessary blocking. Instead of using synchronized methods or otherwise using the lock associated with this, we create two objects solely to provide locks.

(强调我的)

假设你有两个不交叉的变量。所以你想同时从不同的线程访问每一个。你不需要在对象类本身上定义锁,而是在类object上定义锁,如下所示(示例来自第二个Oracle链接):

public class MsLunch {

    private long c1 = 0;
    private long c2 = 0;

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

    public void inc1() {
        synchronized(lock1) {
            c1++;
        }
    }

    public void inc2() {
        synchronized(lock2) {
            c2++;
        }
    }
}

其他回答

你可以像下面这样做。在这种情况下,您使用a和b上的锁来同步,而不是“this”上的锁。我们不能使用int,因为原始值没有锁,所以我们使用Integer。

class x{
   private Integer a;
   private Integer b;
   public void addA(){
      synchronized(a) {
         a++;
      }
   }
   public synchronized void addB(){
      synchronized(b) {
         b++;
      }
   }
}

关于同步方法的“Java™教程”:

首先,对同一对象的同步方法的两次调用不可能交织。当一个线程正在为一个对象执行同步方法时,所有为同一对象调用同步方法的其他线程将暂停执行,直到第一个线程处理完该对象。

关于同步块的“Java™教程”:

Synchronized statements are also useful for improving concurrency with fine-grained synchronization. Suppose, for example, class MsLunch has two instance fields, c1 and c2, that are never used together. All updates of these fields must be synchronized, but there's no reason to prevent an update of c1 from being interleaved with an update of c2 — and doing so reduces concurrency by creating unnecessary blocking. Instead of using synchronized methods or otherwise using the lock associated with this, we create two objects solely to provide locks.

(强调我的)

假设你有两个不交叉的变量。所以你想同时从不同的线程访问每一个。你不需要在对象类本身上定义锁,而是在类object上定义锁,如下所示(示例来自第二个Oracle链接):

public class MsLunch {

    private long c1 = 0;
    private long c2 = 0;

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

    public void inc1() {
        synchronized(lock1) {
            c1++;
        }
    }

    public void inc2() {
        synchronized(lock2) {
            c2++;
        }
    }
}

被访问的锁在对象上,而不是方法上。在方法中访问哪些变量无关紧要。

在方法中添加"synchronized"意味着运行代码的线程必须在继续之前获得对象上的锁。添加“static synchronized”意味着运行代码的线程必须在继续之前获取类对象上的锁。或者,你也可以像这样将代码封装在一个块中:

public void addA() {
    synchronized(this) {
        a++;
    }
}

这样您就可以指定必须获取其锁的对象。

如果你想避免锁定包含对象,你可以选择:

使用指定不同锁的同步块 使a和b具有原子性(使用java.util.concurrent.atomic)

这个例子(虽然不是很漂亮)可以提供更多关于锁定机制的见解。如果incrementA是同步的,而incrementB没有同步,那么incrementB将尽快执行,但如果incrementB也是同步的,那么它必须“等待”incrementA完成,然后incrementB才能完成它的工作。

这两个方法都被调用到单个实例对象上,在这个例子中是:job,而“竞争”线程是aThread和main。

尝试在incrementB中使用'synchronized'和不使用它,你会看到不同的结果。如果incrementB也是“同步”的,那么它必须等待incrementA()完成。每个变体运行几次。

class LockTest implements Runnable {
    int a = 0;
    int b = 0;

    public synchronized void incrementA() {
        for (int i = 0; i < 100; i++) {
            this.a++;
            System.out.println("Thread: " + Thread.currentThread().getName() + "; a: " + this.a);
        }
    }

    // Try with 'synchronized' and without it and you will see different results
    // if incrementB is 'synchronized' as well then it has to wait for incrementA() to finish

    // public void incrementB() {
    public synchronized void incrementB() {
        this.b++;
        System.out.println("*************** incrementB ********************");
        System.out.println("Thread: " + Thread.currentThread().getName() + "; b: " + this.b);
        System.out.println("*************** incrementB ********************");
    }

    @Override
    public void run() {
        incrementA();
        System.out.println("************ incrementA completed *************");
    }
}

class LockTestMain {
    public static void main(String[] args) throws InterruptedException {
        LockTest job = new LockTest();
        Thread aThread = new Thread(job);
        aThread.setName("aThread");
        aThread.start();
        Thread.sleep(1);
        System.out.println("*************** 'main' calling metod: incrementB **********************");
        job.incrementB();
    }
}

这可能行不通,因为从Integer到int的装箱和自动装箱依赖于JVM,如果两个不同的数字在-128和127之间,很可能会被散列到相同的地址。