Re: Synchronized Editing Context for Locking/Unlocking
Re: Synchronized Editing Context for Locking/Unlocking
- Subject: Re: Synchronized Editing Context for Locking/Unlocking
- From: Jean-Francois Veillette <email@hidden>
- Date: Fri, 3 Sep 2010 11:10:11 -0400
Le 2010-09-03 à 10:56, Ken Anderson a écrit :
> I emailed Kieran the same question directly.... I lock prior to the try block personally.
>
> To me, it's stylistic, since it's going to block in any case. Maybe someone on the list has a different perspective?
Either way, just be sure that once it's lock(), you get the unlock(). And make sure that you can only get the unlock() when you locked first.
#1 lock outside ...
ec.lock();
System.out.print(myIvar); // BADLY PLACED
try {
// System.out.print(myIvar); // would be correct here
...
} finally {
ec.unlock();
}
you might never unlock ... if the myIvar.toString() throw an exception.
#2 lock inside ...
// System.out.print(myIvar); // would be correct here
try {
System.out.print(myIvar); // BADLY PLACED
ec.lock();
...
} finally {
ec.unlock();
}
you might never lock() but always try to unlock() ... if the myIvar.toString() throw an exception.
There is no perferct solution, just make sure there is nothing in between the lock and the try so that neither case 1 or 2 happend.
> On Sep 3, 2010, at 10:54 AM, John Bruce wrote:
>
>> Hi Kieran,
>>
>> Just wondering - what is the difference between having the lock inside
>> the try versus outside? Is it just to ensure that it is locked before
>> doing anything with the context? I've always locked inside the try
>> block as the first statement but I notice others lock outside the try.
>> I always assumed this was a style preference but is there a technical
>> reason to lock outside.
>>
>> Cheers,
>>
>> John
>>
>>
>> On Fri, Sep 3, 2010 at 1:50 PM, Kieran Kelleher <email@hidden> wrote:
>>> You need variation of usage #1
>>>
>>> editingContext().lock();
>>> try {
>>>
>>> // Do your stuff
>>>
>>> } finally {
>>> editingContext().unlock();
>>> }
>>>
>>>
>>>
>>> On Sep 3, 2010, at 7:59 AM, Farrukh Ijaz wrote:
>>>
>>>> Hi,
>>>>
>>>> What is the difference between the two? I noticed both work almost the same way.
>>>>
>>>> Usage 1:
>>>>
>>>> try {
>>>> editingContext().lock();
>>>> // Do your stuff
>>>> } finally {
>>>> editingContext().unlock();
>>>> }
>>>>
>>>> Usage 2:
>>>>
>>>> synchronized(editingContext()) {
>>>> // Do your stuff
>>>> }
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden