gpt4 book ai didi

java - 关于从同步块(synchronized block)中调用方法

转载 作者:搜寻专家 更新时间:2023-11-01 02:21:34 27 4
gpt4 key购买 nike

同步一个方法是否等同于只让一个线程对其求值,直到它超出包括内部方法调用在内的范围?

例如:

public synchronized void foo(){

if(critical condition){
bar(); // can influence the above condition
}
baz(); // can influence the above condition
}

bar 中是否可以有两个线程(假设它只从这里调用)?

如果 baz 可以从代码中除 foo 之外的其他地方调用,那么它可以有两个线程吗?

最佳答案

Can two threads be in bar (suppose it's called only from here)?

是的,只要他们使用不同的对象,或者一个是 wait()ing。

What if baz can be called from another place in the code other than foo, can two threads be in it then?

是的,将 synchronized 放在一个方法上对未同步的方法没有影响。

关于java - 关于从同步块(synchronized block)中调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40072018/

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