由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Apply lock on a class.
相关主题
Java synchronized method一问请教:怎么把synchronize的method改成用thread 但thread safe呢?
Talk a little more about How to lock a file请教一个多线程lock机制的问题
发现 synchronized 的一个问题Java concurrency的疑惑,难道我理解错了? (转载)
一个关于generics的问题同一个Lock锁两次,性能比较差
synchronized method does lock the object that passed into the method as a parameter?看到一个关于singleton的面试题
CC150 16.6答案是不是有问题? (转载)今天下午要面一个老印
thread safe Singleton 的几种方法?新手问个multi-threading关于synchronized和volatile的问题
java ,wait ,notify notifyall索尼面经 (兼问第十道题)
相关话题的讨论汇总
话题: class话题: static话题: apply话题: member
进入Java版参与讨论
1 (共1页)
i**p
发帖数: 902
1
The synchronized method is applied the instance of a class.
If I need to synchronize for the class, could anyone here send me a
reference?
For example, the class A has 2 instances, a1 and a2. The thread 1 is running
on a1, and thread 2 is running on a2. I expect the 2 threads can be
synchronized. We may need to use static member(s) on the class A, but do we
have to?
r*****l
发帖数: 2859
2
You should be able to use
synchronized (A.class).
However, why don't you want to use static member for the lock?

running
we

【在 i**p 的大作中提到】
: The synchronized method is applied the instance of a class.
: If I need to synchronize for the class, could anyone here send me a
: reference?
: For example, the class A has 2 instances, a1 and a2. The thread 1 is running
: on a1, and thread 2 is running on a2. I expect the 2 threads can be
: synchronized. We may need to use static member(s) on the class A, but do we
: have to?

i**p
发帖数: 902
3
Thanks! synchronized (A.class) does help. However, we still need a static
data member to help.
For example, "available" has to be a static to be shared by all the A's
instance.
synchronized (A.class) {
while (available == false)
A.class.wait();
....
}
I am trying to find if it is possible to set locks on a class by not using
static members.

【在 r*****l 的大作中提到】
: You should be able to use
: synchronized (A.class).
: However, why don't you want to use static member for the lock?
:
: running
: we

r*****l
发帖数: 2859
4
If "available" has business meaning and you have business logic to set it,
then you should create such property.

【在 i**p 的大作中提到】
: Thanks! synchronized (A.class) does help. However, we still need a static
: data member to help.
: For example, "available" has to be a static to be shared by all the A's
: instance.
: synchronized (A.class) {
: while (available == false)
: A.class.wait();
: ....
: }
: I am trying to find if it is possible to set locks on a class by not using

i**p
发帖数: 902
5
Is it possible to set locks on a class by not using any
static member? Can anyone here show me an example, a reference or a link?

【在 r*****l 的大作中提到】
: If "available" has business meaning and you have business logic to set it,
: then you should create such property.

r*****l
发帖数: 2859
6
You already did that:
synchronized (A.class)

【在 i**p 的大作中提到】
: Is it possible to set locks on a class by not using any
: static member? Can anyone here show me an example, a reference or a link?

1 (共1页)
进入Java版参与讨论
相关主题
索尼面经 (兼问第十道题)synchronized method does lock the object that passed into the method as a parameter?
[合集] Java interview question, Thanks.CC150 16.6答案是不是有问题? (转载)
interview question:thread safe Singleton 的几种方法?
昨天面试的一道题java ,wait ,notify notifyall
Java synchronized method一问请教:怎么把synchronize的method改成用thread 但thread safe呢?
Talk a little more about How to lock a file请教一个多线程lock机制的问题
发现 synchronized 的一个问题Java concurrency的疑惑,难道我理解错了? (转载)
一个关于generics的问题同一个Lock锁两次,性能比较差
相关话题的讨论汇总
话题: class话题: static话题: apply话题: member