Semaphore-like method in j2se 1.4 ? (FileChannel and FileLock)

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
ryno71Asked:
Who is Participating?
 
arun_kuttzCommented:
actually most of the additions to 1.5's concurrency and semaphore classes were borrowed from
util.concurrency package written by Doug Lee.. he was responsible for JSR166 which brought about the improvements in Java 1.5

http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html

-KuTtZ
0
 
CEHJCommented:
Semaphores are not inter-process communications - they are inter-thread (or logical thread)

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/
0
 
Tommy BraasCommented:
>> but this doesn't deal well with many processes running on the same machine
If you're trying to accomplish this on the machine level, not just JVM level, then you will need native code to ensure the lock. But then only if the operating system supports it.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Giant2Commented:
0
 
Giant2Commented:
Sorry not help you.
0
 
ryno71Author Commented:
Giant2

Thanks for the feedback anyway!  Appreciate it

ryno71
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.