aspose file tools
The moose likes Threads and Synchronization and the fly likes Only enter synchronized block if condition is true Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Only enter synchronized block if condition is true" Watch "Only enter synchronized block if condition is true" New topic
Author

Only enter synchronized block if condition is true

John Wright
Greenhorn

Joined: Jun 14, 2001
Posts: 6
What is the best design pattern for the following problem: You have a section of code that must be synchronized. Most of the time when someone enters your method that contains this code the condition that determines whether to enter this code will be false. If the condition check is outside the synchronized block 2 threads could run through the body of the synchronized block of code at the one after another if they both evaluate the condition to true. Also while the synchronized code is being executed you don't want other threads to excecute inside the method. Is there a way to achieve this behaviour without synchronizing the condition check. For e.g. the following won't work if you only want to rollover the logs once and you don't want any thread logging during the time the logs are being rolled over.
public class Log
{
public static log(String msg)
{
if (currentTime > timeToRollover)
{
synchronized (Log.class)
{
//write a msg out to the old log, create a new one
// and log a msg to the new one
}
}
logMsg(msg);
}
However the following has poor performance because threads must contend for the monitor to check for rollover time.
public class Log
{
public static log(String msg)
{
synchronized (Log.class)
{
if (currentTime > timeToRollover)
{
//write a msg out to the old log, create a new one
// and log a msg to the new one
}
}
logMsg(msg);
}
Girish Pednekar
Greenhorn

Joined: Aug 10, 2001
Posts: 14
U might want to use the double-checked locking which is a mix of both scenarios. If 2 or more threads get thru the first 'if' simutaneously, only one will enter the 2nd 'if'. the remaining threads will enter the synchronized block but not the 2nd 'if'.

There are instances when double-checked locking may fail e.g. during lazy instantiation. for more details click here. But in your case I believe it should work. Also u might want to synchronize the logMsg(msg) on the Log.Class to make sure no thread is logging a message while the other thread is in the 2nd 'if' creating a new log.
thanks,
GP
[This message has been edited by Girish Pednekar (edited August 15, 2001).]
John Wright
Greenhorn

Joined: Jun 14, 2001
Posts: 6
Originally posted by Girish Pednekar:
[B]U might want to use the double-checked locking which is a mix of both scenarios. If 2 or more threads get thru the first 'if' simutaneously, only one will enter the 2nd 'if'. the remaining threads will enter the synchronized block but not the 2nd 'if'.

There are instances when double-checked locking may fail e.g. during lazy instantiation. for more details click here. But in your case I believe it should work.
thanks,
GP[/B]

Thanks for the suggestion. This may prevent 2 threads from doing the rollover but how do you prevent threads from logging during the rollover.
Girish Pednekar
Greenhorn

Joined: Aug 10, 2001
Posts: 14
synchronize the logMsg(msg) similar to the 'if' statement. I believe that should take care of it. the performance problem would still remain though. how about something like below. see if this works.

thanks,
GP
[This message has been edited by Girish Pednekar (edited August 15, 2001).]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Only enter synchronized block if condition is true
 
Similar Threads
threads concurrency problem
Threads 001
Runaway Thread
Threading synchronization
Attn.Kyle:Problem while configuring WPS with Sun One Directory Server!!