何时使用Java中同步 [英] When to use synchronized in Java
问题描述
我希望这将是足够的信息,所以在这里不言而喻。如果您需要更多的信息,还是让我知道在评论。
I hope this is going to be enough information, so here it goes. If you need more info, lemme know in the comments.
我有了两个内部类的类。该内部类各有调用在外部类中的方法两种方法。因此,它看起来是这样的:
I have a class that has two inner classes. The inner classes each have two methods that call a method in the outer class. So, it looks like this:
public OuterClass {
private boolean outerMethodHasBeenCalled = false;
private void outerMethod() {
if(!outerMethodHasBeenCalled) {
// do stuff
}
outerMethodHasBeenCalled = true;
}
private FirstInnerClass {
public void someMethod() {
outerMethod();
}
}
private SecondInnerClass {
public void someOtherMethod() {
outerMethod();
}
}
}
要注意,这是很重要的:
It's important to note that:
- 这是一个Android应用程序。对
FirstInnerClass
和SecondInnerClass
传递给web视图作为一个JavaScript接口,所以的someMethod <实例/ code>和
someOtherMethod
可随时调用,没有特定的顺序。 - 在目前,我有与现有的code的一个问题(没有synchronized关键字),其中
outerMethod
叫做pretty的多少在完全相同的时间(我打印出一个日志消息,他们正在时间戳到第二第1000)由不同的对象。我的应用程序,然后做的东西两次,因为outerMethodHasBeenCalled
还是假的,当outerMethod
被调用。这是不行的,这正是我想要prevent。我的应用程序只能做的东西一次且仅一次:第一次outerMethod
叫 。
- 这听起来像我有
OuterClass
的多个实例,但请放心,它的OuterClass
只有一个实例。
- This is for an Android app. Instances of
FirstInnerClass
andSecondInnerClass
are passed to a WebView as a JavaScript interface, sosomeMethod
andsomeOtherMethod
can be called at any time, in no particular order. - I currently have a problem with the existing code (without the synchronized keyword) where
outerMethod
is called pretty much at the exact same time (I print out a log message, and they're timestamped to the 1000th of a second) by different objects. My app then 'does stuff' twice becauseouterMethodHasBeenCalled
is still false whenouterMethod
was called. This is not okay, and it is exactly what I'm trying to prevent. My app should only 'do stuff' once and only once: the first timeouterMethod
is called. - It might sound like I have multiple instances of
OuterClass
, but rest assured that it's only one instance ofOuterClass
.
重要的是,我的应用做的东西只在第一次 outerMethod
被调用(我希望这是显而易见的现在)。所有后续调用基本上是忽略不计。无论内部类调用 outerMethod
第一 - 无所谓
It's important that my app 'does stuff' only the first time outerMethod
gets called (I hope that's evident by now). All subsequent calls are essentially ignored. Whichever inner class calls outerMethod
first -- doesn't matter.
那么,是不是适合使用synchronized关键字在这种情况下?
So, is it appropriate to use the synchronized keyword in this case?
推荐答案
是啊,给你上面已经摆出来,我会跟去:
Yup, given what you've laid out above, I'd go with:
private synchronized void outerMethod() {
...
}
请注意,这将有阻塞呼叫者直到outerMethod之一的副作用()完成。如果这是可以接受的,冷却。如果目的仅仅是,code在outerMethod()被运行一次,和的是确定了第二个呼叫者不被延迟,如果第一个来电正在运行outerMethod(),你可能会考虑:
Note, this will have the side-effect of blocking one of the callers until the outerMethod() completes. If that is acceptable, cool. If the intent is merely that the code in outerMethod() is run once, and it is OK for the second caller not to be delayed if the first caller is running outerMethod(), you might consider:
public OuterClass {
private AtomicBoolean outerMethodHasBeenCalled = new AtomicBoolean();
private void outerMethod() {
if (outerMethodHasBeenCalled.compareAndSet(false, true)) {
// do stuff
}
}
...
请参见 JavaDoc中的AtomicBoolean 神交是怎么回事那里(假设它是在Android的Java中可用)。
See the JavaDoc for AtomicBoolean to grok what is going on there (assuming it is available in Android's Java).
这篇关于何时使用Java中同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!