This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
PF3/hw1/Ex5/src/main/java/reentrantrwlock/ReaderSimulation.java

35 lines
856 B
Java

package reentrantrwlock;
import java.util.List;
import java.util.Random;
public class ReaderSimulation implements Runnable {
// Maximum time taken by a user to read a single title
private static final int MAX_READING_TIME = 10;
private static final Random generator = new Random();
private final MovieCatalog myCatalog;
public ReaderSimulation(MovieCatalog myCatalog) {
this.myCatalog = myCatalog;
}
private void read() throws Exception {
int time = generator.nextInt(MAX_READING_TIME);
Thread.sleep(time);
}
public void run() {
try {
List<String> titles = myCatalog.getTitles();
for (int i = 0; i < titles.size(); i++) {
read();
}
System.out.println("[" + Thread.currentThread().getName()
+ "]: I've finished reading movie titles");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}