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

Posted on 2006-05-03
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(){
      public RynoLock(int rulock){
      public synchronized void acquirerulock(){
            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

      public synchronized void releaserulock(){
// Release the lock

              // Close the file



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?  

Question by:ryno71
    LVL 86

    Expert Comment

    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:
    LVL 14

    Expert Comment

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

    Assisted Solution

    LVL 3

    Accepted Solution

    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

    LVL 12

    Expert Comment

    LVL 12

    Expert Comment

    Sorry not help you.

    Author Comment


    Thanks for the feedback anyway!  Appreciate it


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    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…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now