Is it a general rule to avoid using if statements in a synchronized block,
and use a while instead to prevent problems associated with slicing in and out of threads.
I have coded an unlock method which uses if statements to check preconditions as the following psudo code outlines...
note: recordsLocked is a hashmap containing record number/lock pairs
synchronized( recordsLocked )
{
// Check if lock exists for this record
if(! recordExistsInHash )
{
throw ...
}
// Check if client has correct lock
if(! recordMatchCookie )
{
throw ...
}
// Has lock, now unlock record
if( recordExistsInHash && recordMatchCookie )
{
...
}
}
I am using the if statements to check preconditions, which either pass of fail when the method is called, and since it holds the lock on the recordsLocked map, these conditions should not change over slicing ?
Is this bad coding practice?.
Regards
Hugh
and use a while instead to prevent problems associated with slicing in and out of threads.
I have coded an unlock method which uses if statements to check preconditions as the following psudo code outlines...
note: recordsLocked is a hashmap containing record number/lock pairs
synchronized( recordsLocked )
{
// Check if lock exists for this record
if(! recordExistsInHash )
{
throw ...
}
// Check if client has correct lock
if(! recordMatchCookie )
{
throw ...
}
// Has lock, now unlock record
if( recordExistsInHash && recordMatchCookie )
{
...
}
}
I am using the if statements to check preconditions, which either pass of fail when the method is called, and since it holds the lock on the recordsLocked map, these conditions should not change over slicing ?
Is this bad coding practice?.
Regards
Hugh