51 lines
1.7 KiB
Java
51 lines
1.7 KiB
Java
package reentrantrwlock;
|
|
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
import java.util.ArrayList;
|
|
|
|
public class Main {
|
|
// 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;
|
|
// Number of users watching movies
|
|
private static final int NUM_WATCHERS = 150;
|
|
// Number of users reading the catalog
|
|
private static final int NUM_READERS = 100;
|
|
// Number of publishers adding movies
|
|
private static final int NUM_ADDERS = 30;
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
// The shared catalog
|
|
MovieCatalog myCatalog = new MovieCatalog();
|
|
// Populating the catalog with a initial set of movies
|
|
List<Movie> movies = new ArrayList<>(INITIAL_SIZE);
|
|
Random generator = new Random();
|
|
for (int i = 0; i < INITIAL_SIZE; i++) {
|
|
movies.add(
|
|
new Movie("Movie" + i, generator.nextInt(MAX_DURATION)));
|
|
}
|
|
Thread creator = new Thread(
|
|
new CreatorSimulation(myCatalog, movies), "Creator");
|
|
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();
|
|
}
|
|
// Starting the threads simulating the readers of movie titles
|
|
for (int i = 0; i < NUM_READERS; i++) {
|
|
new Thread(new ReaderSimulation(myCatalog), "Reader" + i).start();
|
|
}
|
|
// Starting the threads simulating the publishers adding movies
|
|
for (int i = 0; i < NUM_ADDERS; i++) {
|
|
Movie movie = new Movie("NewMovie" + i, generator.nextInt(MAX_DURATION));
|
|
new Thread(
|
|
new PublisherSimulation(myCatalog, movie), "Publisher" + i).start();
|
|
}
|
|
}
|
|
}
|