Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save monjasa/2069a2d683ef98f450fab71283f6feb1 to your computer and use it in GitHub Desktop.
Save monjasa/2069a2d683ef98f450fab71283f6feb1 to your computer and use it in GitHub Desktop.
<component name="ArtifactManager">
<artifact type="jar" name="semaphore_task:jar">
<output-path>$PROJECT_DIR$/out/artifacts/semaphore_task_jar</output-path>
<root id="archive" name="semaphore_task.jar">
<element id="module-output" name="semaphore_task" />
</root>
</artifact>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/semaphore_task.iml" filepath="$PROJECT_DIR$/semaphore_task.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
public class Book {
private String title;
public Book(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Stack;
import java.util.concurrent.Semaphore;
public class LibraryClient {
private static ArrayList<Reader> readers = new ArrayList<>();
private static ArrayList<Thread> threads = new ArrayList<>();
private static Stack<Book> books = new Stack<>();
private static final int CLASS_COUNT = 150;
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(4);
books.push(new Book("The Hound of the Baskervilles"));
books.push(new Book("The Da Vinci Code"));
books.push(new Book("The Alchemist"));
books.push(new Book("Harry Potters and The Philosopher's Stone"));
for (int i = 1; i < CLASS_COUNT; i++) {
readers.add(new Reader(i, semaphore));
}
try {
for (Reader reader : readers) {
Thread thread = new Thread(reader);
threads.add(thread);
thread.start();
Thread.sleep(250);
}
for (Thread thread : threads)
thread.join();
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
public static Book popBookFromLibrary() {
Book book = null;
try {
book = books.pop();
} catch (EmptyStackException exception) {
System.out.println("There's no book left in the library!");
}
return book;
}
public static void pushBookToLibrary(Book book) {
books.push(book);
}
}
Manifest-Version: 1.0
Main-Class: LibraryClient
import java.util.concurrent.Semaphore;
public class Reader implements Runnable {
int id;
Semaphore semaphore;
public Reader(int id, Semaphore semaphore) {
this.id = id;
this.semaphore = semaphore;
}
@Override
public void run() {
Book book = null;
try {
System.out.println("Student #" + id + " is waiting to enter the library...");
semaphore.acquire();
book = LibraryClient.popBookFromLibrary();
System.out.println("Student #" + id + " has started reading " + book.getTitle() + ".");
for (int i = 0; i < 3; i++) {
Thread.sleep(1000);
System.out.println("Student #" + id + " is reading " + book.getTitle() + " at the moment...");
}
System.out.println("Student #" + id + " has finished reading.");
} catch (InterruptedException exception) {
exception.printStackTrace();
}
System.out.println("Student #" + id + " is leaving...");
LibraryClient.pushBookToLibrary(book);
semaphore.release();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment