This program below is a multithread reader/writers solution program in Java. How
ID: 3715721 • Letter: T
Question
This program below is a multithread reader/writers solution program in Java. How could you modify this program so that it can be come prioritized. Like being able to make either the reader or writer have priority.
import java.util.concurrent.Semaphore;
class MultiThreadReadersWriters {
static Semaphore ReadLock = new Semaphore(1);
static Semaphore WriteLock = new Semaphore(1);
volatile static int readcount = 0;
static class Reader implements Runnable {
@Override
public void run() {
try {
ReadLock.acquire();
readcount++;
if (readcount == 1) {
WriteLock.acquire();
}
ReadLock.release();
System.out.println("Thread " + Thread.currentThread().getName() + " READING");
Thread.sleep(1500);
System.out.println("Thread " + Thread.currentThread().getName() + " DONE READING");
ReadLock.acquire();
readcount--;
if(readcount == 0) {
WriteLock.release();
}
ReadLock.release();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
static class Writer implements Runnable {
@Override
public void run() {
try {
WriteLock.acquire();
System.out.println("Thread "+Thread.currentThread().getName() + " WRITING");
Thread.sleep(2500);
System.out.println("Thread "+Thread.currentThread().getName() + " DONE WRITING");
WriteLock.release();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) throws Exception {
Reader read = new Reader();
Writer write = new Writer();
Thread t1 = new Thread(read);
t1.setName("T 1");
Thread t2 = new Thread(write);
t2.setName("T 2");
Thread t3 = new Thread(read);
t3.setName("T 3");
Thread t4 = new Thread(read);
t4.setName("T 4");
Thread t5 = new Thread(read);
t5.setName("T 5");
Thread t6 = new Thread(read);
t5.setName("T 6");
t1.start();
t3.start();
t2.start();
t4.start();
t5.start();
t6.start();
}
}
Explanation / Answer
Priority can either be given by JVM while creating the thread
or it can be given by programmer explicitly.
The accepted value of priority for a thread is in range of 1 to 10.
setPriority(int newPriority) method is used to set the priority of a thread.
There are 3 static variables defined in Thread class for priority:
1. MIN_PRIORITY: This is the minimum priority that a thread can have.
Value for this is 1.
2. NORM_PRIORITY: This is default priority of a thread if do not explicitly define it. Value for this is 5.
3.MAX_PRIORITY: This is the maximum priority of a thread.
Value for this is 10.
For Ex-
If you want to give max priority to reading then in the main function you would
set the priority of that thread like this:
t1.setPriority(Thread.MAX_PRIORITY); // sets t1 to max priority.
Below is the code with reading as the max priority.The code will perform reading first and after
that only perform the writing.If you want to give write the priority just set the
write threads priority to the max.
import java.util.concurrent.Semaphore;
class MultiThreadReadersWriters {
static Semaphore ReadLock = new Semaphore(1);
static Semaphore WriteLock = new Semaphore(1);
volatile static int readcount = 0;
static class Reader implements Runnable {
@Override
public void run() {
try {
ReadLock.acquire();
readcount++;
if (readcount == 1) {
WriteLock.acquire();
}
ReadLock.release();
System.out.println("Thread " + Thread.currentThread().getName() + " READING");
Thread.sleep(1500);
System.out.println("Thread " + Thread.currentThread().getName() + " DONE READING");
ReadLock.acquire();
readcount--;
if(readcount == 0) {
WriteLock.release();
}
ReadLock.release();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
static class Writer implements Runnable {
@Override
public void run() {
try {
WriteLock.acquire();
System.out.println("Thread "+Thread.currentThread().getName() + " WRITING");
Thread.sleep(2500);
System.out.println("Thread "+Thread.currentThread().getName() + " DONE WRITING");
WriteLock.release();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) throws Exception {
Reader read = new Reader();
Writer write = new Writer();
Thread t1 = new Thread(read);
t1.setPriority(Thread.MAX_PRIORITY);
t1.setName("T 1");
Thread t2 = new Thread(write);
t2.setName("T 2");
Thread t3 = new Thread(read);
t3.setPriority(Thread.MAX_PRIORITY);
t3.setName("T 3");
Thread t4 = new Thread(read);
t4.setName("T 4");
t4.setPriority(Thread.MAX_PRIORITY);
Thread t5 = new Thread(read);
t5.setName("T 5");
t5.setPriority(Thread.MAX_PRIORITY);
Thread t6 = new Thread(read);
t6.setPriority(Thread.MAX_PRIORITY);
t5.setName("T 6");
t1.start();
t3.start();
t2.start();
t4.start();
t5.start();
t6.start();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.