Done Ex5 but untestes

This commit is contained in:
Claudio Maggioni (maggicl) 2019-10-21 01:05:19 +02:00
parent 4badbec943
commit ae127b3240
8 changed files with 69 additions and 8 deletions

36
hw1/Ex5/pom.xml Normal file
View file

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.usi.inf.pf3.hw1</groupId>
<artifactId>ex5</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>reentrantrwlock.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -31,6 +31,7 @@ public class Main {
creator.start(); creator.start();
creator.join(); creator.join();
System.out.println("The movie catalog has been created."); System.out.println("The movie catalog has been created.");
// Starting the threads simulating the movie watchers // Starting the threads simulating the movie watchers
for (int i = 0; i < NUM_WATCHERS; i++) { for (int i = 0; i < NUM_WATCHERS; i++) {
new Thread(new WatcherSimulation(myCatalog), "Watcher" + i).start(); new Thread(new WatcherSimulation(myCatalog), "Watcher" + i).start();

View file

@ -7,6 +7,7 @@ import java.util.TreeMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.Iterator;
public class MovieCatalog { public class MovieCatalog {
@ -27,7 +28,11 @@ public class MovieCatalog {
* @return the size of this catalog * @return the size of this catalog
*/ */
public int getSize() { public int getSize() {
// TODO: Implement method getSize int length;
rLock.lock();
length = movies.size();
rLock.unlock();
return length;
} }
/** /**
@ -37,7 +42,11 @@ public class MovieCatalog {
* null if the movie is not in the catalog * null if the movie is not in the catalog
*/ */
public Movie getMovie(String title) { public Movie getMovie(String title) {
//TODO: Implement method getMovie Movie m;
rLock.lock();
m = movies.get(title);
rLock.unlock();
return m;
} }
/** /**
@ -45,7 +54,11 @@ public class MovieCatalog {
* @return a list of the movie titles in this catalog * @return a list of the movie titles in this catalog
*/ */
public List<String> getTitles() { public List<String> getTitles() {
//TODO: Implement method getTitles List<String> titles;
rLock.lock();
titles = new ArrayList<>(movies.keySet());
rLock.unlock();
return titles;
} }
/** /**
@ -53,7 +66,16 @@ public class MovieCatalog {
* @return a randomly selected movie from this catalog * @return a randomly selected movie from this catalog
*/ */
public Movie getRandomMovie() { public Movie getRandomMovie() {
// TODO: Implement method getRandomMovie Movie m;
rLock.lock();
final int r = generator.nextInt(movies.size()) - 1;
final Iterator<Movie> it = movies.values().iterator();
for (int i = 0; i < r; i++) {
it.next();
}
m = it.next();
rLock.unlock();
return m;
} }
/** /**
@ -61,6 +83,8 @@ public class MovieCatalog {
* @param the movie to be added to the catalog * @param the movie to be added to the catalog
*/ */
public void addMovie(Movie movie) { public void addMovie(Movie movie) {
//TODO: Implement method addMovie wLock.lock();
movies.put(movie.getTitle(), movie);
wLock.unlock();
} }
} }