Multi-threaded locking data

Category: Java SE
 
HNWPHNNWP
2009-03-20 03:53:27

Sponsored Links
class B_Factory
{
List <B> bs;
}

class B
{
C c1;
C c2;
}

class C
{
}

class D
{
void f ()
{
remove B from A object, call the object's method to modify B c1, c2
as well as an increase in the delete operation on B_Factory.bs
}
}

4 classes of relations such as code schematically. Multi-threaded environment, D.f () is concurrent calls. Since B C object objects are shared (two pairs of one), you need to f () locking. But the problem is, if f () in the lock, it means that even concurrent threads can also be blocked (eg A.bs there are two B object, the first object that corresponds to B 2 C object, the second B Object C objects corresponding to another two, four C objects are different. then when f () is the first B manipulated object, the second object B f () operation can be executed concurrently, without waiting for the first a B object operation is completed)

way I can think of is to f () to B class to go to achieve, but from the perspective of the code is not realistic, f (), there are right B_Factory.bs increase the delete operation, into B will be very troublesome.
or the f () to move B_Factory, but, and in f () is the same as in the lock, the same time, only one object B is operated, even if the back is blocked, C and B are now being operated entirely It does not matter, they have to wait.

from java provides a mechanism, it seems that only the f () into B to solve the problem?

Sponsored Links

anmissyou
2009-03-20 04:05:36
can display lock ReentranLock and ReentrantReadWriteLock
iamkade
2009-03-20 04:21:34
You did lock the D object, an instance method on this object is locked
ym0459
2009-03-20 04:41:21
There is one thing I do not know
class D
{
B b;
synchronized void f ()
{
remove object B from A, B object method call to modify c1, c2
as well as an increase in deletion operation on B_Factory.bs
}
}

synchronized or ReentrantLock is b locked object or all objects of class B? If only locked object is b, then only those and the current blocking access to the same lock Db D objects.
lajiwangzhan100
2009-03-20 04:57:46
I understand, in the B to add a LOCK object, let Df () to obtain this lock object, and then locked to
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.