Link to home
Start Free TrialLog in
Avatar of matthew016
matthew016Flag for Belgium

asked on

Synchronize question

Hi,

I have a class calling IndexSearcherManager,
it has a set of synchronized methods, because it is a web application (so multi-threaded environment) and
these methods do operations on a file system.

But sometimes in my code I need to 'lock' the access to this file system.

I want the requests to these synchronized methods been waiting until I unlock it again.

How can I do this ?

Thank u.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(or if you have a Producer/Consumer situation, you can try this):

http://today.java.net/pub/a/today/2004/09/15/sync2.html
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mnrz
mnrz

you can use something like this:

for example you have a method to get a Searcher:

public Searchable getSearcher(){
   synchronized(this){
        while(directoryIsLocked || writerInUse){
                 try{
                        wait();
                 }catch(InterruptedException e){
                          //notify() called
                  }
         }

          //rest of the codes....
        notifyAll();
        return a_searcher;

  }

}

directoryIsLocked is a boolean that will set to false when the file is unlocked
writerInUse is another boolean when you are wrting to index for example.
you can put other boolean variables there

hope this help