matthew016
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(InterruptedExceptio n 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
for example you have a method to get a Searcher:
public Searchable getSearcher(){
synchronized(this){
while(directoryIsLocked || writerInUse){
try{
wait();
}catch(InterruptedExceptio
//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
http://today.java.net/pub/a/today/2004/09/15/sync2.html