Redian新闻
>
Amazon一道synchronization的面试题
avatar
Amazon一道synchronization的面试题# JobHunting - 待字闺中
f*t
1
对synchronization没啥基础。看了看career cup。
. Give a class foo, inside, it has a synchronized method. We have two
objects of foo, called, f1 and f2, if f1 and f2 are concurrently running,
can we guarantee the synchronized method being accessed by only one thread?
答案是不是yes?因为method已经是synchronized了,只能被一个thread所执行?
avatar
b*d
2
俺模糊的记得synchronization要synchronize一个object. default 的object 是this.
f1 和 f2 是不同的object, 所以答案是no.不知道对不对。
avatar
w*9
3
synchronization is access control for multi threads on a single object
unless it is synchronized static method. so the synchronized method on
different objects CAN be accessed by different thread on it, because each
thread receives its own object lock.
avatar
g*y
4
u r right

this.

【在 b******d 的大作中提到】
: 俺模糊的记得synchronization要synchronize一个object. default 的object 是this.
: f1 和 f2 是不同的object, 所以答案是no.不知道对不对。

avatar
f*t
5
嗯,那这种情况下,是不是说synchronize了f1,对f2没效果?
同理,synchronize了f2,对f1没效果?

this.

【在 b******d 的大作中提到】
: 俺模糊的记得synchronization要synchronize一个object. default 的object 是this.
: f1 和 f2 是不同的object, 所以答案是no.不知道对不对。

avatar
f*t
6
刚看了一个例子。大概是说static synchronized method是对class object lock,
synchronized method是对当前object lock。如果都是static synchronized或者都是
synchronized,则可以实现synchronization,因为是对同一个object。
那么在这个例子里面,f1和f2的两个synchronized是对不同的object lock。要让它们
真正synchronized,函数得用static synchronization。
以上是我的观点,不知对不对?
Using synchronized keyword along with method is easy just apply synchronized
keyword in front of method. What we need to take care is that static
synchronized method locked on class object lock and non static synchronized
method locks on current object (this). So it’s possible that both static
and non static java synchronized method running in parallel. This is the
common mistake a naive developer do while writing java synchronized code.
public class Counter{
private static count = 0;
public static synchronized getCount(){
return this.count;
}
public synchoronized setCount(int count){
this.count = count;
}
}
In this example of java synchronization code is not properly synchronized
because both getCount() and setCount() are not getting locked on same object
and can run in parallel which results in getting incorrect count. Here
getCount() will lock in Counter.class object while setCount() will lock on
current object (this). To make this code properly synchronized in java you
need to either make both method static or non static or use java
synchronized block instead of java synchronized method.
avatar
g*e
7
wrong. synchroize keyword locks the object.

?

【在 f**********t 的大作中提到】
: 对synchronization没啥基础。看了看career cup。
: . Give a class foo, inside, it has a synchronized method. We have two
: objects of foo, called, f1 and f2, if f1 and f2 are concurrently running,
: can we guarantee the synchronized method being accessed by only one thread?
: 答案是不是yes?因为method已经是synchronized了,只能被一个thread所执行?

avatar
h*d
8
有一种synchronization可以on class, 那样答案可以是yes
avatar
g*e
9
make the method static, or add a static object member then synchronize the
object.

【在 h**********d 的大作中提到】
: 有一种synchronization可以on class, 那样答案可以是yes
avatar
h*d
10
不完全是
by synchronizing the static method you ensure that any two threads trying to
access the data will be prevented from simultaneous access, because both
threads will have to acquire locks on the Class object for the class the
static method's defined in.
这些情况可以弄的很复杂,前提是static data 用 static methods 去 access

【在 g**e 的大作中提到】
: wrong. synchroize keyword locks the object.
:
: ?

avatar
g*e
11
I mean in his question that synchronize keyword locks object only.

to

【在 h**********d 的大作中提到】
: 不完全是
: by synchronizing the static method you ensure that any two threads trying to
: access the data will be prevented from simultaneous access, because both
: threads will have to acquire locks on the Class object for the class the
: static method's defined in.
: 这些情况可以弄的很复杂,前提是static data 用 static methods 去 access

avatar
x*i
12
以下为标准答案:
非static 方法 lock 当前对象。
static方法lock 该class.
所以只有两个方法都是static才可以同步,否则不行。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。