Done Ex5 but untestes
This commit is contained in:
parent
4badbec943
commit
ae127b3240
8 changed files with 69 additions and 8 deletions
36
hw1/Ex5/pom.xml
Normal file
36
hw1/Ex5/pom.xml
Normal 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>
|
|
@ -5,7 +5,7 @@ import java.util.Random;
|
|||
import java.util.ArrayList;
|
||||
|
||||
public class Main {
|
||||
// Initial number of movies in the catalog
|
||||
// Initial number of movies in the catalog
|
||||
private static final int INITIAL_SIZE = 500;
|
||||
// Maximum duration of a movie in the catalog
|
||||
private static final int MAX_DURATION = 3000;
|
||||
|
@ -21,7 +21,7 @@ public class Main {
|
|||
MovieCatalog myCatalog = new MovieCatalog();
|
||||
// Populating the catalog with a initial set of movies
|
||||
List<Movie> movies = new ArrayList<>(INITIAL_SIZE);
|
||||
Random generator = new Random();
|
||||
Random generator = new Random();
|
||||
for (int i = 0; i < INITIAL_SIZE; i++) {
|
||||
movies.add(
|
||||
new Movie("Movie" + i, generator.nextInt(MAX_DURATION)));
|
||||
|
@ -31,6 +31,7 @@ public class Main {
|
|||
creator.start();
|
||||
creator.join();
|
||||
System.out.println("The movie catalog has been created.");
|
||||
|
||||
// Starting the threads simulating the movie watchers
|
||||
for (int i = 0; i < NUM_WATCHERS; i++) {
|
||||
new Thread(new WatcherSimulation(myCatalog), "Watcher" + i).start();
|
|
@ -7,6 +7,7 @@ import java.util.TreeMap;
|
|||
import java.util.ArrayList;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class MovieCatalog {
|
||||
|
||||
|
@ -27,7 +28,11 @@ public class MovieCatalog {
|
|||
* @return the size of this catalog
|
||||
*/
|
||||
public int getSize() {
|
||||
// TODO: Implement method getSize
|
||||
int length;
|
||||
rLock.lock();
|
||||
length = movies.size();
|
||||
rLock.unlock();
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,15 +42,23 @@ public class MovieCatalog {
|
|||
* null if the movie is not in the catalog
|
||||
*/
|
||||
public Movie getMovie(String title) {
|
||||
//TODO: Implement method getMovie
|
||||
Movie m;
|
||||
rLock.lock();
|
||||
m = movies.get(title);
|
||||
rLock.unlock();
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of all movie titles in this catalog
|
||||
* @return a list of the movie titles in this catalog
|
||||
* @return a list of the movie titles in this catalog
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public void addMovie(Movie movie) {
|
||||
//TODO: Implement method addMovie
|
||||
wLock.lock();
|
||||
movies.put(movie.getTitle(), movie);
|
||||
wLock.unlock();
|
||||
}
|
||||
}
|
Reference in a new issue