Java辅导 | CSI3131 – Lab4 Objectives Thread Synchronization (Java)

本次为CSI3131辅导Lab4之Java多线程同步代码之实现

CSI3131 – Lab4
Objectives Thread Synchronization (Java)
To gain experience with Java semaphores in creating a simulation of a Readers/Writers application. Writers requires exclusive access to the database, readers can access the database concurrently.
You will implement the solution for synchronization Readers/Writers problem similar to Assignment2 Q2a.
Extends the java Thread class. In its run function it attempts a number of reads, before it ends. Every read cycle is a sequence of:
If a writer want to get access to the database, no more readers are allowed access till all writers that want access are allowed to write. In this solution readers will always read the most up-to date data, but may suffer from starvation.
The Reader class
1. pre-read synchronization,
2. read – actually sleep to spend time (don’t touch), and 3. post-read synchronization
Extends the java Thread class. In its run function it attempts a number of reads, before it ends. Every read cycle is a sequence of:
Your task is to implement the pre_read & post_read functions.
The Writer class
1. pre-write synchronization,
2. write – actually sleep to spend time (don’t touch), and 3. post-write synchronization
The SyncConstruct class includes the required semaphores/mutex required to solve the synchronization problem. It’s a singleton class that is provided to all readers & writes threads. The proposed solution is shown below:
Your task is to implement the pre_write & post_write functions.
The Synchronization Construct Class

Writer
Reader
a. wait(wrMutex) b. if (nWriters>0){
a. nWriters ++
b. signal(wrMutex) c. wait(allowWr)
}
c. else {
a. nWriters ++
b. signal(wrMutex) }
d. wait(dbMutex)
e. database write
f. signal(dbMutex)
g. wait(wrMutex)
h. nWriters–
i. if(nWriters>0)signal(allowWr) j. elsesignal(allowRd)
k. signal(wrMutex)
a. wait(wrMutex)
b. if (nWriters > 0){
a. signal(wrMutex)
b. wait(allowRd) }
c. else signal(wrMutex)
d. wait(rdMutex)
e. if (nReaders=0) wait(dbMutex) f. nReaders++
g. signal rdMutex
h. database-read
i. waitrdMutex
j. nReaders–
k. if (nReaders=0) signal(dbMutex) l. signalrdMutex
Provided constructs:
1. dbMutex: used to provide exclusive access to the database
2. rdMutex: used to provide exclusive access to nReaders shared data 3. wrMutex: used to provide exclusive access to nWriters shared data 4. allowWr: semaphore used to allow only one writer to attempt write 5. allowRd: can be implemented in two ways
SS
S
S
W
W
W
W
W
S

o Semaphore: will require an additional shared data nWaitingReaders, to allow all pending reading concurrent access to the database
The ReadersWritersApp is a console application that builds the application. It initializes the Reades & Writers and starts their threads to run. You can try different number of readers & writers from the command line. The default is 30 Readers and 10 Writers. You should not touch this file.
To compile the cosole application: javac *.java
Your task is to develop the above solution in the Reader & Writer classes. Provide useful logging messages to explain what is going on: e.g. sync.logger.log(“Writer sync.allowRd.release ” + nWaitingReaders);
o Wait/notify-all: does not require additional constructs
The Readers/Writes Application
To run the application: java ReadersWritersApp [readers] [wtiters] To enable assertions: java -ea ReadersWritersApp [readers] [wtiters]
Your Task
I added some assertions to verify the validity of your solution, you should not touch those assertions. If you think of any more assertions to add, send me email – there could be bonus for that.