Skip to content

Commit 4bbf277

Browse files
committed
Student library stimulation
1 parent 47a6680 commit 4bbf277

File tree

8 files changed

+135
-0
lines changed

8 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.ashfaq.dev.problems.StudentLibrarySimulation;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
public class App {
7+
8+
public static void main(String[] args) throws InterruptedException {
9+
10+
Student[] students = null;
11+
Book[] books = null;
12+
ExecutorService executorService = Executors.newFixedThreadPool(Constants.NUM_OF_STUDENTS);
13+
14+
try {
15+
students = new Student[Constants.NUM_OF_STUDENTS];
16+
books = new Book[Constants.NUM_OF_BOOKS];
17+
18+
for (int i = 0; i < Constants.NUM_OF_BOOKS; i++) {
19+
books[i] = new Book(i + 1);
20+
}
21+
22+
for (int i = 0; i < Constants.NUM_OF_STUDENTS; i++) {
23+
students[i] = new Student(i + 1, books);
24+
executorService.execute(students[i]);
25+
}
26+
27+
} catch (Exception e) {
28+
// TODO: handle exception
29+
} finally {
30+
executorService.shutdown();
31+
}
32+
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.ashfaq.dev.problems.StudentLibrarySimulation;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
public class Book {
8+
9+
private int id;
10+
private Lock lock;
11+
12+
public Book(int id) {
13+
this.id = id;
14+
this.lock = new ReentrantLock();
15+
}
16+
17+
public void read(Student student) throws InterruptedException {
18+
// lock.tryLock(10, TimeUnit.MILLISECONDS);
19+
lock.lock();
20+
System.out.println(student + " starts reading " + this);
21+
Thread.sleep(2000);
22+
lock.unlock();
23+
System.out.println(student + " has just finsnished reading " + this);
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "Book-" + this.id;
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.ashfaq.dev.problems.StudentLibrarySimulation;
2+
3+
public class Constants {
4+
private Constants() {
5+
// TODO Auto-generated constructor stub
6+
}
7+
8+
public static final int NUM_OF_STUDENTS = 5;
9+
public static final int NUM_OF_BOOKS = 7;
10+
// public static final int SIMULATION_RUNNING_TIME = 1000;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.ashfaq.dev.problems.StudentLibrarySimulation;
2+
3+
import java.util.Random;
4+
5+
public class Student implements Runnable {
6+
private int id;
7+
private Book[] books;
8+
9+
private Random random;
10+
11+
public Student(int id, Book[] books) {
12+
this.id = id;
13+
this.books = books;
14+
this.random = new Random();
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "Student [id=" + id + "]";
20+
}
21+
22+
public int getId() {
23+
return id;
24+
}
25+
26+
public void setBooks(Book[] books) {
27+
this.books = books;
28+
}
29+
30+
public void setId(int id) {
31+
this.id = id;
32+
}
33+
34+
public Book[] getBooks() {
35+
return books;
36+
}
37+
38+
@Override
39+
public void run() {
40+
while (true) {
41+
int bookId = random.nextInt(Constants.NUM_OF_BOOKS);
42+
try {
43+
books[bookId].read(this);
44+
} catch (InterruptedException e) {
45+
// TODO Auto-generated catch block
46+
e.printStackTrace();
47+
}
48+
}
49+
}
50+
51+
52+
53+
54+
55+
56+
57+
58+
}

0 commit comments

Comments
 (0)