- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
据我了解下面的代码,在 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,这是一个 mutex
在synchronized
堵塞。这是正确的吗?
问题 3:如果我们需要对多个对象进行修改,那么进一步假设是否正确,this
是 mutex
我们应该使用什么?
例如:
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/
我正在实现 IMAP 客户端,但 IMAP 邮箱同步出现问题。 首先,可以从 IMAP 服务器获取新邮件,但我不知道如何从邮箱中查找已删除的邮件。 我是否应该从服务器获取所有消息并将其与本地数据进行比
我研究线程同步。当我有这个例子时: class A { public synchronized void methodA(){ } public synchronized void met
嗨,我做了一个扩展线程的东西,它添加了一个包含 IP 的对象。然后我创建了该线程的两个实例并启动它们。他们使用相同的列表。 我现在想使用 Synchronized 来阻止并发更新问题。但它不起作用,我
我正在尝试使用 FTP 定期将小数据文件从程序上传到服务器。用户从使用 javascript XMLHttpRequest 函数读取数据的网页访问数据。这一切似乎都有效,但我正在努力解决由 FTP 和
我不知道如何同步下一个代码: javascript: (function() { var s2 = document.createElement('script'); s2.src =
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
一 点睛 1 Message 在基于 Message 的系统中,每一个 Event 也可以被称为 Message,Message 是对 Event 更高一个层级的抽象,每一个 Message 都有一个
一 点睛 1 Message 在基于 Message 的系统中,每一个 Event 也可以被称为 Message,Message 是对 Event 更高一个层级的抽象,每一个 Message 都有一个
目标:我所追求的是每次在数据库中添加某些内容时(在 $.ajax 到 Submit_to_db.php 之后),从数据库获取数据并刷新 main.php(通过 draw_polygon 更明显)。 所
我有一个重复动画,需要与其他一些 transient 动画同步。重复动画是一条在屏幕上移动 4 秒的扫描线。当它经过下面的图像时,这些图像需要“闪烁”。 闪烁的图像可以根据用户的意愿来来去去和移动。它
我有 b 个块,每个块有 t 个线程。 我可以用 __syncthreads() 同步特定块中的线程。例如 __global__ void aFunction() { for(i=0;i #
我正在使用azure表查询来检索分配给用户的所有错误实体。 此外,我更改了实体的属性以声明该实体处于处理模式。 处理完实体后,我将从表中删除该实体。 当我进行并行测试时,可能会发生查询期间,一个实体已
我想知道 SQLite 是如何实现它的。它基于文件锁定吗?当然,并不是每个访问它的用户都锁定了整个数据库;那效率极低。它是基于多个文件还是仅基于一个大文件? 如果有人能够简要概述一下 sqlite 中
我想post到php,当id EmpAgree1时,然后它的post变量EmpAgree=1;当id为EmpAgree2时,则后置变量EmpAgree=2等。但只是读取i的最后一个值,为什么?以及如何
CUBLAS 文档提到我们在读取标量结果之前需要同步: “此外,少数返回标量结果的函数,例如 amax()、amin、asum()、rotg()、rotmg()、dot() 和 nrm2(),通过引用
我知道下面的代码中缺少一些内容,我的问题是关于 RemoteImplementation 中的同步机制。我还了解到该网站和其他网站上有几个关于 RMI 和同步的问题;我在这里寻找明确的确认/矛盾。 我
我不太确定如何解决这个问题......所以我可能需要几次尝试才能正确回答这个问题。我有一个用于缓存方法结果的注释。我的代码目前是一个私有(private)分支,但我正在处理的部分从这里开始: http
我对 Java 非常失望,因为它不允许以下代码尽可能地并发移动。当没有同步时,两个线程会更频繁地切换,但是当尝试访问同步方法时,在第二个线程获得锁之前以及在第一个线程获得锁之前再次花费太长时间(比如
过去几周我一直在研究java多线程。我了解了synchronized,并理解synchronized避免了多个线程同时访问相同的属性。我编写此代码是为了在同一线程中运行两个线程。 val gate =
我有一个关于 Java 同步的简单问题。 请假设以下代码: public class Test { private String address; private int age;
我是一名优秀的程序员,十分优秀!