gpt4 book ai didi

java - 关于 Java 同步块(synchronized block)的 3 个快速问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:35:01 25 4
gpt4 key购买 nike

据我了解下面的代码,在 synchronized block ,this是一个计数器的实例。

问题 1:在下面的示例中,这是否意味着当线程 A 到达 synchronized 时? block ,线程 B 被阻止对 Counter? 的实例做任何事情。换句话说,这是否意味着 Threads 可以继续按照他们所希望的方式执行,但是一旦达到 synchronized block ,另一个停止对类做任何事情直到 block 退出?

public class Counter {

public void increment() {
// Some code

synchronized (this) { // <---- "this" is an instance of Counter
// Some more code
}
}
}

比较上面的代码

public class Counter {

List<String> listOfStrings = new ArrayList<String>();

public void increment() {
// Some code

synchronized (listOfStrings) {
// Some code that deals with
// listOfStrings
}
}
}

问题2:在上面的例子中,一旦线程A到达synchronized block ,线程 B 可以继续读取和写入类中的任何内容,但 listOfStrings 除外。 ArrayList,这是一个 mutexsynchronized堵塞。这是正确的吗?

问题 3:如果我们需要对多个对象进行修改,那么进一步假设是否正确,thismutex我们应该使用什么?

例如:

public class Counter {

List<String> listOfStrings = new ArrayList<String>();
List<Integers> listOfIntegers = new ArrayList<Integers>();

public void increment() {
// Some code

synchronized (this) {
// Some code that deals with
// listOfStrings and listOfIntegers
}
}
}

我理解正确吗?如果我说错了什么,请更正。

最佳答案

Thread B is blocked from doing anything to the instance of a Counter?

不,线程 B 被阻止进入同步代码块,它仍然可以进入其他方法:那些不同步的和那些使用不同对象同步的。线程 B 不能只访问使用已被不同线程获取的对象同步的 block (同步锁是可重入的)。

Thread B may continue reading and writing anything in the class with the exception of listOfStrings

并非如此,listOfStrings 在一个 synchronized block 中用作互斥锁这一事实并不意味着其他线程无法显式访问该对象。这仅意味着其他线程无法访问由同一对象保护的同步块(synchronized block)。因此,如果您想安全访问 listOfStrings 对象,访问该对象的所有方法都必须同步并使用相同的锁(例如 listOfStrings)。

顺便说一句,你同步的每个对象都应该是 final 以避免麻烦。

Is it further correct to assume that if we need to make modifications to multiple objects, this is the mutex we should use?

是也不是。考虑以下情况:

List<String> listOfStrings = new ArrayList<String>();
List<Integers> listOfIntegers = new ArrayList<Integers>();
Set<String> setOfStrings = new HashSet<String>();
Set<Integers> setOfIntegers = new HashSet<Integers>();

如果一个方法只访问列表而第二个方法只访问集合,你可以安全地使用两个锁——一个用于第一个方法,第二个用于第二个方法。在 this 上同步不会有什么坏处,但会影响性能:

private final Object listLock = new Object();
private final Object setLock = new Object();

及以后:

synchronized (listLock) {  
// Some code that deals with
// setOfStrings and setOfIntegers
}

//...

synchronized (setLock) {
// Some code that deals with
// setOfStrings and setOfIntegers
}

关于java - 关于 Java 同步块(synchronized block)的 3 个快速问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11376637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com