?
Solved

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

Posted on 2006-05-03
7
Medium Priority
?
910 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:ryno71
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16600017
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
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 16600030
>> 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
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 16601129
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
arun_kuttz earned 1000 total points
ID: 16602690
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
 
LVL 12

Expert Comment

by:Giant2
ID: 16603065
0
 
LVL 12

Expert Comment

by:Giant2
ID: 16637873
Sorry not help you.
0
 

Author Comment

by:ryno71
ID: 16638694
Giant2

Thanks for the feedback anyway!  Appreciate it

ryno71
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question