本文介绍了Java同步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

public synchronized void onSignalsTimeout(List<SignalSpec> specs) {
    if (specs != null && specs.size() > 0) {
        for (SignalSpec spec : specs) {
            ParsedCANSignal timeoutedSignal = new ParsedCANSignal();
            SignalsProvider.getInstance().setSignal(spec.name, spec.parent.parent.channel, timeoutedSignal);
        }
    }
}

我有一个简单的问题:
当线程1调用onSignalsTimeout方法时,线程2可以访问在该方法中访问的对象吗?

I've got simple question:When Thread 1 calls onSignalsTimeout method, can Thread 2 access objects that are accessed in that method?

如果'synchronized'仅锁定,则无法找到任何地方访问此方法或访问此方法中使用的所有对象。

Can't find anywhere if 'synchronized' locks only access to this method or access to all objects used in this method.

推荐答案

首先,忘记同步方法。一个所谓的同步方法......

First of all, forget about synchronized methods. A so-called synchronized method...

synchronized AnyType foobar(...) {
    doSomething();
}

只是写这个的快捷方式:

Is nothing but a shortcut way of writing this:

AnyType foobar(...) {
    synchronized(this) {
        doSomething();
    }
}

方法在任何一种情况下。有意义的是同步,同步块的作用非常简单。当JVM执行此操作时:

There is nothing special about the method in either case. What is special is the synchronized block, and what a synchronized block does is very simple. When the JVM executes this:

synchronized(foo) {
    doSomething();
}

它首先计算表达式 foo 。结果必须是对象引用。然后它锁定对象,执行 synchronized 块的主体,然后解锁对象。

It first evaluates the expression foo. The result must be an object reference. Then it locks the object, performs the body of the synchronized block, and then it unlocks the object.

但是锁定意味着什么?这可能意味着比你想象的要少。它阻止其他线程使用该对象。它不会阻止它们访问对象的字段,也不会阻止它们更新其字段。锁定对象唯一阻止的是,它会阻止其他线程同时锁定同一个对象。

But what does locked mean? It may mean less than you think. It does not prevent other threads from using the object. It doesn't prevent them from accessing the object's fields or, from updating its fields. The only thing that locking an object prevents is, it prevents other threads from locking the same object at the same time.

如果线程A试图输入 synchronized(foo){...} 而线程B已经锁定foo(在同一个 synchronized 块中,或者在另一个块中),然后线程A将被强制等待,直到线程B释放锁定。

If thread A tries to enter synchronized(foo) {...} while thread B already has foo locked (either in the same synchronized block, or in a different one), then thread A will be forced to wait until thread B releases the lock.

您使用 synchronized 块以保护数据

假设您的程序有一些可以在不同的对象集合状态。假设某些状态有意义,但还有其他状态没有意义 - 无效状态。

Suppose your program has some collection of objects that can be in different states. Suppose that some states make sense, but there are other states that don't make sense—invalid states.

假设不可能一个线程将数据从一个有效状态更改为另一个有效状态,而暂时创建一个无效状态。

Suppose that it is not possible for a thread to change the data from one valid state to another valid state without temporarily creating an invalid state.

如果你把代码放在那里更改 synchronized(foo)块中的状态,然后将每个代码块状态进入同步块,锁定同一个对象 foo ,然后你将阻止其他线程看到临时无效状态。

If you put the code that changes the state in a synchronized(foo) block, and you put every block of code that can see the state into a synchronized block that locks the same object, foo, then you will prevent other threads from seeing the temporary invalid state.

这篇关于Java同步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:17