hi i want some change in this code. let one writer, one reader at the same time. Writer has preference.

package test;

import java.util.concurrent.Semaphore;

class ReaderWritersProblem {

    static Semaphore readLock = new Semaphore(1);
    static Semaphore writeLock = new Semaphore(1);
    volatile static int readCount = 0;

    static class Read implements Runnable {
        @Override
        public synchronized void run() {
            try {
                readLock.acquire();
                readCount++;
                if (readCount == 1) {
                    writeLock.acquire();
                }
                System.out.println("Thread "+Thread.currentThread().getName() + " is READING");
                Thread.sleep(1500);
                System.out.println("Thread "+Thread.currentThread().getName() + " has FINISHED READING");
                readLock.release();
                readCount--;
                if(readCount == 0) {
                    writeLock.release();
                }
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    static class Write implements Runnable {
        @Override
        public synchronized void run() {
            try {
                writeLock.acquire();
                System.out.println("Thread "+Thread.currentThread().getName() + " is WRITING");
                Thread.sleep(2500);
                System.out.println("Thread "+Thread.currentThread().getName() + " has finished WRITING");
                writeLock.release();
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Read read = new Read();
        Write write = new Write();
        Thread t1 = new Thread(read);
        t1.setName("thread1");
        Thread t2 = new Thread(read);
        t2.setName("thread2");
        Thread t3 = new Thread(write);
        t3.setName("thread2");
        Thread t4 = new Thread(read);
        t4.setName("thread4");
        t1.run();
        t2.run();
        t3.run();
        t4.run();
    }
}

Recommended Answers

All 3 Replies

That's not how most forums work. If you were putting up a job for hire that's one thing but here you did a short intro without enough detail. Think about your classes in system desing or others where you make your software design documents. You omitted not only that but may not know this forum well. That is, you share the issue you are having with the minimum viable example. But "hey, can you change this random code I found on the internet?" That's done when you put up a job and its pay.

commented: and the writer be high priority +0

So just start one writer and one reader thread instead of the 3 readers in that code.
Set the priority of the threads to give the priority you want.

What's weird about this code is that it creates theads, but just executes their run methods in the current thead.
Try calling start for the Theads on lines 60-63 instead of run

And? What's stopping you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.