Avatar of ryno71
ryno71

asked on 

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
Java

Avatar of undefined
Last Comment
ryno71
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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/
Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

>> 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.
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of arun_kuttz
arun_kuttz

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Giant2
Giant2

Avatar of Giant2
Giant2

Sorry not help you.
Avatar of ryno71
ryno71

ASKER

Giant2

Thanks for the feedback anyway!  Appreciate it

ryno71
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo