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?
|
|