synchronized function access
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Abhinav_Srivastava
Posted On:   Thursday, September 12, 2002 09:13 AM

hi..
i make a static synchronized function that writes to a file.
So that i DON'T have two processes tryin to write into the same file.
Now..one process waits for the other to complete. But what i want is that instead of waiting, if the method is in use..an exception is thrown..
Is this possible.
Or can this be done in any other way..

thanx a lot.

Re: synchronized function access

Posted By:   Zac_Jacobson  
Posted On:   Thursday, September 12, 2002 12:34 PM

Instead of synchronizing the entire method, you need to have synchronized enter and exit methods that get accessed from your write method. Also, make sure that the code to change the variable back to "unlocked" is in a finally block that belongs to a try block which contains the enter method.

  1. Enter write method
    1. Enter synchronized enter method
      • Is file marked as being locked?
      • Yes:
        1. throw exception
        No:
        1. Mark file as locked
    2. Write to file
    3. Enter synchronized exit method
      1. Make file as being unlocked
About | Sitemap | Contact