Question: package sync; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; public class Test { public static void main ( String [ ] args ) { ExecutorService executorService

package sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class Test
{
public static void main
(
String
[
]
args
)
{
ExecutorService executorService
=
Executors.newCachedThreadPool
(
)
;
ReadWriteLock RW
=
new ReadWriteLock
(
)
;
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
)
)
;
}
}
class ReadWriteLock
{
private Semaphore S
=
new Semaphore
(
1
)
;
public void readLock
(
)
{
}
public void writeLock
(
)
{
}
public void readUnLock
(
)
{
}
public void writeUnLock
(
)
{
}
}
class Writer implements Runnable
{
private ReadWriteLock RW
_
lock;
public Writer
(
ReadWriteLock rw
)
{
RW
_
lock
=
rw;
}
public void run
(
)
{
while
(
true
)
{
RW
_
lock.writeLock
(
)
;
RW
_
lock.writeUnLock
(
)
;
}
}
}
class Reader implements Runnable
{
private ReadWriteLock RW
_
lock;
public Reader
(
ReadWriteLock rw
)
{
RW
_
lock
=
rw;
}
public void run
(
)
{
while
(
true
)
{
RW
_
lock.readLock
(
)
;
RW
_
lock.readUnLock
(
)
;
}
}
}
this is the test.java mentioned in the question.can you do the to do part ?In this project you will need to provide a solution to readers-writers problem in which several processes (readers and
writers) are trying to access shared variables. Obviously, if two readers access the shared data simultaneously, no
adverse effects will result, hence, they are allowed to access. However, if a writer and some other process (either a
reader or a writer) access the data simultaneously, chaos may ensue. To ensure that these difficulties do not arise,
we require that the writers have exclusive access to the shared data while writing to the data.
The solution must guarantee that:
If a writer has begun writing process, then
No additional writer can perform write function
No reader is allowed to read
If 1 or more readers are reading, then
Other readers may read as well
No writer may perform write function until all readers have finished reading
You are given Test class written in Java that use ReadWriteLock class and threads for the problem. You are
expected to use Semaphore provided in the code.
Two operations on the semaphore is allowed; acquire() and release()(they correspond wait and signal functions)
To do: Implement methods of ReadWriteLock class given.
class ReadWriteLock {
private Semaphore S?= new Semaphore(1);
public void readLock(){
}
public void writeLock(){
}
public void readUnLock(){
}
public void writeUnLock(){
}
}
 package sync; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; public class Test

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!