Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Reader Writer Project

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reader Writer Project

    Hi guys, this is my code but I got some error. I think I made a mistake in main part. Buto I dont know how can I fix it. Anyone can help me?
    I have an error in executer part. These are my errors:
    The constructor Writer(ReadWriteLock) is undefined
    The constructor Reader(ReadWriteLock) is undefined
    Also this is my code:

    public class ReaderWriter{

    public static void main(String args[]){

    ReadWriteLock RW = new ReadWriteLock();
    ExecutorService executorService = Executors.newCachedThreadPool();

    executorService.execute(new Writer(RW));
    executorService.execute(new Writer(RW));
    executorService.execute(new Writer(RW));
    executorService.execute(new Writer(RW));

    executorService.execute(new Reader(RW));
    executorService.execute(new Reader(RW));
    executorService.execute(new Reader(RW));
    executorService.execute(new Reader(RW));

    }
    }
    //Writer class
    class Writer implements Runnable
    {
    private ReadWriteLock rwlock;
    private int writerNum;

    public Writer(int w, ReadWriteLock rw) {
    writerNum = w;
    rwlock = rw;
    }

    public void run() {
    while (true){
    SleepUtilities.nap();

    System.out.println("writer " + writerNum + " wants to write.");
    rwlock.WriteLock(writerNum);

    SleepUtilities.nap();

    rwlock.WriteUnLock(writerNum);
    }
    }
    }

    //Reader class
    class Reader implements Runnable
    {

    private ReadWriteLock rwlock;
    private int readerNum;

    public Reader(int readerNum, ReadWriteLock database) {
    this.readerNum = readerNum;
    this.rwlock = database;
    }

    public void run() {
    while (true) {
    SleepUtilities.nap();
    System.out.println("reader " + readerNum + " wants to read.");
    rwlock.ReadLock(readerNum);

    // you have access to read from the database
    // let's read for awhile .....
    SleepUtilities.nap();

    rwlock.ReadUnLock(readerNum);
    }
    };
    }

    //Lock class
    class ReadWriteLock {
    private int readerCount;
    private Semaphore mutex;
    private Semaphore rwlock;

    public ReadWriteLock() {
    readerCount = 0;
    mutex = new Semaphore(1);
    rwlock = new Semaphore(1);
    }

    public void ReadLock(int readerNum) {
    try{
    mutex.acquire();
    }
    catch (InterruptedException e) {

    }
    ++readerCount;


    if (readerCount == 1){
    try{
    rwlock.acquire();
    }
    catch (InterruptedException e) {

    }
    }

    System.out.println("Reader " + readerNum + " is reading. Reader count = " + readerCount);
    mutex.release();
    }

    public void ReadUnLock(int readerNum) {
    try{
    mutex.acquire();
    }
    catch (InterruptedException e) {}

    --readerCount;

    if (readerCount == 0){
    rwlock.release();
    }

    System.out.println("Reader " + readerNum + " is done reading. Reader count = " + readerCount);

    mutex.release();
    }

    public void WriteLock(int writerNum) {
    try{
    rwlock.acquire();
    }
    catch (InterruptedException e) {}
    System.out.println("Writer " + writerNum + " is writing.");
    }

    public void WriteUnLock(int writerNum) {
    System.out.println("Writer " + writerNum + " is done writing.");
    rwlock.release();
    }
    }
    class SleepUtilities
    {
    public static void nap() {
    nap(NAP_TIME);
    }

    public static void nap(int duration) {
    int sleeptime = (int) (NAP_TIME * Math.random() );
    try { RW.sleep(sleeptime*1000); }
    catch (InterruptedException e) {}
    }

    private static final int NAP_TIME = 5;
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reader Writer Project

    hese are my errors:
    The constructor Writer(ReadWriteLock) is undefined
    The constructor Reader(ReadWriteLock) is undefined
    The compiler can not find a definition for the constructors shown in the error messages that take the arguments. Make sure there is a matching constructor in the class
    or do not use the argument when calling the constructor.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] XML parser and writer not working.
    By GoodbyeWorld in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 7th, 2013, 07:54 PM
  2. Apprending a print writer file
    By ajw1993 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 08:45 AM
  3. Print Writer Problem
    By ajw1993 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 16th, 2013, 03:50 PM
  4. Buferred Writer in TextFile
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2013, 02:37 AM
  5. [SOLVED] Buffered writer in a .jar file
    By lorider93p in forum Java Theory & Questions
    Replies: 8
    Last Post: February 8th, 2012, 01:03 PM