Professional Documents
Culture Documents
et de la recherche Scientifique
*** * ***
Université de Carthage
*** * ***
Institut National des Sciences
Appliquées et de Technologie
______________________________________________________________________
Compte Rendu TP 2
Les sémaphores avec JAVA
Réalisé par :
CHAKER Haythem
RIAHI Hadhami
Exercice 1 :
Classe ConsommateurVector.java :
2
Classe producteur Vector.java:
3
Test.java
4
Exécution :
5
Configuration Exercice 2
Processeur : Intel Core i7-7700HQ CPU @2.80GHz
Système : Windows 10 Professionnel 64bits
RAM : 16,0 Go
Java :
Exercice 2
1. package aaptp3;
2.
3. import java.util.Scanner;
4. import java.util.concurrent.Semaphore;
5. import java.util.logging.Level;
6. import java.util.logging.Logger;
7.
8. public class AAPTP3 {
9.
10. private static Counter waiting = new Counter(0); //Number of waiting passangers
11. private static Semaphore mutex = new Semaphore(1); //To lock the the boarding process
12. private static Semaphore bus = new Semaphore(0); //To tell the bus is at the station
13. private static Semaphore boarded = new Semaphore(0); //To board passengers one by one
14.
15. public static void main(String[] args) {
16.
17. Scanner sc = new Scanner(System.in);
18.
19. System.out.print("(Number of passengers ?) N = ");
20. int n = sc.nextInt();
21. System.out.println("");
22.
23. System.out.print("(Number of seats?) C = ");
24. int c = sc.nextInt();
25. System.out.println("");
26.
27. new Bus(n, waiting, mutex, bus, boarded, c).start();
28.
29. //Arrival of n riders
30. for (int i = 0; i < n; i++) {
31. new Rider(i, waiting, mutex, bus, boarded).start();
32. }
33.
34. }
35. }
36.
37. class Bus extends Thread {
38.
39. private Counter waiting;
40. private Semaphore mutex;
6
41. private Semaphore bus;
42. private Semaphore boarded;
43. private int c;
44.
45. private int countNumberOfRidersWent;
46.
47. public Bus(int countNumberOfRidersWent, Counter waiting, Semaphore mutex, Semaphore bus, Semaphore
boarded, int c) {
48. this.waiting = waiting;
49. this.mutex = mutex;
50. this.bus = bus;
51. this.boarded = boarded;
52. this.countNumberOfRidersWent = countNumberOfRidersWent;
53. this.c = c;
54. }
55.
56. @Override
57. public void run() {
58.
59. while (true) {
60. try {
61. mutex.acquire(); //start of boarding process
62. System.out.println("\nBus locked the BusStop");
63. } catch (InterruptedException ex) {
64. Logger.getLogger(Bus.class.getName()).log(Level.SEVERE, null, ex);
65. }
66. int n = Math.min(waiting.getCount(), c);
67. //int n = c;
68.
69. countNumberOfRidersWent -= n; // this is to easily demonstrate the problem
70. System.out.println("Available Passengers = " + waiting.getCount() + " and " + n + " will
be boarded...");
71. for (int i = 0; i < n; i++) {
72. bus.release(); // signal bus is ready to get a passenger in
73. try {
74. boarded.acquire(); // rider has boarded
75. } catch (InterruptedException ex) {
76. Logger.getLogger(Bus.class.getName()).log(Level.SEVERE, null, ex);
77. }
78. }
79. //When all the riders have boarded, the bus updates waiting
80. waiting.setCount(Math.max(waiting.getCount() - c, 0));
81.
82. mutex.release(); //end of boarding process
83.
84. System.out.println("Bus depart...\n");
85.
86. // block 'check'
87. if (countNumberOfRidersWent == 0) {
88. break;
89. }
90. }
91. }
92.
93. }
94.
95. class Rider extends Thread {
96.
97. private int id;
98. private Counter waiting;
99. private Semaphore mutex;
7
100. private Semaphore bus;
101. private Semaphore boarded;
102.
103. public Rider(int id, Counter waiting, Semaphore mutex, Semaphore bus, Semaphore boarded) {
104. this.waiting = waiting;
105. this.mutex = mutex;
106. this.bus = bus;
107. this.boarded = boarded;
108. this.id = id;
109. }
110.
111. @Override
112. public void run() {
113.
114. try {
115. mutex.acquire(); //request to board
116. } catch (InterruptedException ex) {
117. Logger.getLogger(Rider.class.getName()).log(Level.SEVERE, null, ex);
118. }
119. waiting.incrementCount();
120. mutex.release();
121. try {
122. bus.acquire(); // waiting for the bus
123. } catch (InterruptedException ex) {
124. Logger.getLogger(Rider.class.getName()).log(Level.SEVERE, null, ex);
125. }
126. System.out.println("rider " + id + " got in to the bus..");
127. boarded.release(); // got in to the bus
128.
129. }
130. }
131.
132. class Counter {
133.
134. private int count;
135.
136. public Counter(int count) {
137. this.count = count;
138. }
139.
140. public int getCount() {
141. return count;
142. }
143.
144. // reasign a value to the counter
145. public void setCount(int count) {
146. this.count = count;
147. }
148.
149. // add 1 to the present value
150. public void incrementCount() {
151. this.count = ++count;
152. }
153. }
8
Exécution 1:
run:
(Number of passengers ?) N = 50
(Number of seats?) C = 10
9
Bus depart...
10
rider 5 got in to the bus..
rider 29 got in to the bus..
rider 6 got in to the bus..
rider 3 got in to the bus..
rider 27 got in to the bus..
rider 9 got in to the bus..
rider 13 got in to the bus..
rider 22 got in to the bus..
rider 36 got in to the bus..
rider 7 got in to the bus..
Bus depart...
Exécution 2:
run:
(Number of passengers ?) N = 50
(Number of seats?) C = 10
11
Bus locked the BusStop
Available Passengers = 0 and 0 will be boarded...
Bus depart...
12
rider 17 got in to the bus..
rider 18 got in to the bus..
rider 19 got in to the bus..
rider 20 got in to the bus..
rider 21 got in to the bus..
rider 23 got in to the bus..
rider 22 got in to the bus..
rider 24 got in to the bus..
rider 25 got in to the bus..
rider 26 got in to the bus..
Bus depart...
13