Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TP Train
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Package registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CELERIER Auguste
TP Train
Commits
d5dbc7a0
Commit
d5dbc7a0
authored
4 months ago
by
CELERIER Auguste
Browse files
Options
Downloads
Patches
Plain Diff
exo 3
parent
7f4d291e
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/train/Railway.java
+21
-7
21 additions, 7 deletions
src/train/Railway.java
src/train/Section.java
+19
-15
19 additions, 15 deletions
src/train/Section.java
with
40 additions
and
22 deletions
src/train/Railway.java
+
21
−
7
View file @
d5dbc7a0
...
@@ -12,6 +12,9 @@ public class Railway {
...
@@ -12,6 +12,9 @@ public class Railway {
private
final
Element
[]
elements
;
private
final
Element
[]
elements
;
private
Direction
currentDirection
=
null
;
//Paramètre pour controller la direction actuelle des trains en circulation
private
Direction
currentDirection
=
null
;
//Paramètre pour controller la direction actuelle des trains en circulation
private
int
movingTrains
=
0
;
//Nombre de trains en circulations
private
int
movingTrains
=
0
;
//Nombre de trains en circulations
private
int
trainsWaitingRL
=
0
;
private
int
trainsWaitingLR
=
0
;
public
Railway
(
Element
[]
elements
)
{
public
Railway
(
Element
[]
elements
)
{
if
(
elements
==
null
)
if
(
elements
==
null
)
...
@@ -55,13 +58,24 @@ public class Railway {
...
@@ -55,13 +58,24 @@ public class Railway {
}
}
public
synchronized
void
requestToMove
(
Train
t
,
Direction
d
)
throws
InterruptedException
{
public
synchronized
void
requestToMove
(
Train
t
,
Direction
d
)
throws
InterruptedException
{
while
(
movingTrains
>
0
&&
currentDirection
!=
d
)
{
// Augmente le nombre de trains en attente
wait
();
// On attend si des trains circulent dans l'autre sens
if
(
d
==
Direction
.
LR
)
trainsWaitingLR
++;
}
else
trainsWaitingRL
++;
// Autorisation de départ
currentDirection
=
d
;
// Attente si un train circule déjà en sens inverse
movingTrains
++;
while
(
movingTrains
>
0
&&
currentDirection
!=
d
&&
(
trainsWaitingRL
<=
trainsWaitingLR
))
{
}
wait
();
}
// Réduit le nombre de trains en attente
if
(
d
==
Direction
.
LR
)
trainsWaitingLR
--;
else
trainsWaitingRL
--;
// Met à jour la direction actuelle
currentDirection
=
d
;
movingTrains
++;
}
public
synchronized
void
trainStopped
(
Train
t
)
{
public
synchronized
void
trainStopped
(
Train
t
)
{
movingTrains
--;
movingTrains
--;
...
...
This diff is collapsed.
Click to expand it.
src/train/Section.java
+
19
−
15
View file @
d5dbc7a0
...
@@ -18,22 +18,26 @@ public class Section extends Element {
...
@@ -18,22 +18,26 @@ public class Section extends Element {
}
}
public
synchronized
void
enterSection
(
Train
t
,
Direction
d
,
Railway
railway
)
throws
InterruptedException
{
public
synchronized
void
enterSection
(
Train
t
,
Direction
d
,
Railway
railway
)
throws
InterruptedException
{
railway
.
requestToMove
(
t
,
d
);
railway
.
requestToMove
(
t
,
d
);
while
(
currentTrain
!=
null
||
(
currentDirection
!=
null
&&
currentDirection
!=
d
))
{
wait
();
// Attente si la section est occupée ou si la direction est opposée
while
(
currentTrain
!=
null
||
(
currentDirection
!=
null
&&
currentDirection
!=
d
))
{
}
wait
();
currentTrain
=
t
;
}
currentDirection
=
d
;
}
currentTrain
=
t
;
currentDirection
=
d
;
}
public
synchronized
void
leaveSection
(
Train
t
,
Railway
railway
)
{
if
(
currentTrain
==
t
)
{
currentTrain
=
null
;
currentDirection
=
null
;
notifyAll
();
// Réveille les trains bloqués
}
railway
.
trainStopped
(
t
);
}
public
synchronized
void
leaveSection
(
Train
t
,
Railway
railway
)
{
if
(
currentTrain
==
t
)
{
currentTrain
=
null
;
currentDirection
=
null
;
// Réinitialiser la direction si la section est vide
notifyAll
();
// Réveille les trains en attente
}
railway
.
trainStopped
(
t
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment