Is there any way in j2SE in 1.4.2 to simulate a semaphore?
I was planning on on implementing the following
public class RynoLock()
{
private int lock;
private File file = new File("Rynolock.txt");
private FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
private FileLock lock;
public RynoLock(){
lock=0;
}
public RynoLock(int rulock){
this.lock=rulock;
}
public synchronized void acquirerulock(){
while(lock==0)
try{
wait();
}
catch)InterruptedException ie){}
//Obtain lock without blocking. Method returns
//returns a null or throws an exception if file is locked.
try {
lock = channel.tryLock();
} catch (OverlappingFileLockException e) {
// File is already locked in this thread or vm
wait();
}
lock--;
}
public synchronized void releaserulock(){
// Release the lock
lock.release();
// Close the file
channel.close();
++lock;
notify();
}
}
but this doesn't deal well with many processes running on the same machine... FileChannel only deals PER JVM right?
What would you suggest using? I would love to use j2se 5.0 with the additions to concurrency and the semaphore class, but need to stick with j2se 1.4.2... Any suggestions? Is there any way of force releasing? I've searchign some, but can't find anything....
serializing/deserializing an object on the server?
thanks
ryno71
It's not necessary to use 1.5 to get a semaphore - you can make one with Object.wait and Object.notify
FileLocks are only advisory and may be ignored
This will discuss semaphores:
http://java.sun.com/docs/books/tutorial/essential/threads/