Results 1 to 2 of 2
  1. #1
    jdubya is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default reader/writer problem with semaphore - compile errors

    I'm fairly new to working with Java and trying to understand this readers writers problem. I copied and pasted some code I found online at Reader/Writer Lock using Semaphores | Syndetic Ideologies just to test its output (there is none without users reading from and writing to it I guess). The code won't compile. The original author put in a third semaphore (to eliminate starvation I think) which wasn't declared. I commented it out but I still have many compile errors. I really don't understand it enough to fix it. It has no comments. Any help would be appreciated. Thanks.
    Java Code:
    import java.util.concurrent.Semaphore;
    public class ReaderWriterLock {
    
      private Semaphore readLock;
      private Semaphore readWriteLock;
      //private Semaphore readLockUnlock;
      private int readers;
    
      public ReaderWriterLock() {
        readers = 0;
        readLock = new Semaphore(1);
        readWriteLock = new Semaphore(1);
    * }
    * public void writeLock() throws InterruptedException {
        readWriteLock.acquire();
    * }
    * public void writeUnlock() throws InterruptedException {
    *  readWriteLock.release();
    * }
    * /*public void readLock() throws InterruptedException {
    *   readLockUnlock.acquire();
    *   if(readers == 0) {
    *     readWriteLock.acquire();
    *   } 
    *   readers++;
    *   readLockUnlock.release(); 
    * } */
      public void readUnlock() throws InterruptedException {
    *   assert readers > 0;
    //*   readLockUnlock.acquire();** *
    *   readers--;
        if(readers == 0) {
    *     readWriteLock.release();
    *   }   
    //*   readLockUnlock.release();
      }
    }
    Errors:
    $ javac semaEx.java
    semaEx.java:13: error: illegal character: \160
    * }
    ^
    semaEx.java:14: error: illegal character: \160
    * public void writeLock() throws InterruptedException {
    ^
    semaEx.java:16: error: illegal character: \160
    * }
    ^
    semaEx.java:17: error: illegal character: \160
    * public void writeUnlock() throws InterruptedException {
    ^
    semaEx.java:18: error: illegal character: \160
    * readWriteLock.release();
    ^
    semaEx.java:19: error: illegal character: \160
    * }
    ^
    semaEx.java:20: error: illegal character: \160
    * /*public void readLock() throws InterruptedException {
    ^
    semaEx.java:29: error: illegal character: \160
    * assert readers > 0;
    ^
    semaEx.java:30: error: illegal character: \160
    * readLockUnlock.acquire();** *
    ^
    semaEx.java:30: error: illegal character: \160
    * readLockUnlock.acquire();** *
    ^
    semaEx.java:30: error: illegal character: \160
    * readLockUnlock.acquire();** *
    ^
    semaEx.java:30: error: illegal character: \160
    * readLockUnlock.acquire();** *
    ^
    semaEx.java:31: error: illegal character: \160
    * readers--;
    ^
    semaEx.java:31: error: ';' expected
    * readers--;
    ^
    semaEx.java:33: error: illegal character: \160
    * readWriteLock.release();
    ^
    semaEx.java:34: error: illegal character: \160
    * }
    ^
    semaEx.java:35: error: illegal character: \160
    * readLockUnlock.release();
    ^
    17 errors
    Last edited by jdubya; 06-13-2014 at 05:47 AM. Reason: error in post

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: reader/writer problem with semaphore - compile errors

    Hi,

    If you check the lines of the errors, then at least I see "*" in front of the code. That is of course not valid.

    In the error message you even are pointed to that illegal character, so right now I am not telling you anything new.
    Maybe you should check the basic java syntax so you know the syntax of java?

    Konrad

Similar Threads

  1. SetterGetter won't compile without errors?
    By doreilly15 in forum New To Java
    Replies: 4
    Last Post: 02-08-2012, 06:51 AM
  2. help with compile errors
    By bzupnick in forum New To Java
    Replies: 1
    Last Post: 02-15-2011, 08:14 PM
  3. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  4. compile errors (how do I fix)
    By rhythmiccycle in forum New To Java
    Replies: 4
    Last Post: 01-03-2011, 10:33 PM
  5. [SOLVED] reader and writer on same file handle
    By Nicholas Jordan in forum Advanced Java
    Replies: 11
    Last Post: 07-01-2008, 03:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •