Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
PP-03-Synchronization
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohamadbashar.disoki
PP-03-Synchronization
Commits
172ae79c
Commit
172ae79c
authored
Nov 15, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parking Cash Example
parent
49282f9a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
148 additions
and
0 deletions
+148
-0
Main.java
src/main/java/ParkingCash/Main.java
+33
-0
ParkingCash.java
src/main/java/ParkingCash/ParkingCash.java
+26
-0
ParkingStats.java
src/main/java/ParkingCash/ParkingStats.java
+56
-0
Sensor.java
src/main/java/ParkingCash/Sensor.java
+33
-0
No files found.
src/main/java/ParkingCash/Main.java
0 → 100644
View file @
172ae79c
package
ParkingCash
;
import
java.util.concurrent.*
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
ParkingCash
cash
=
new
ParkingCash
();
ParkingStats
stats
=
new
ParkingStats
(
cash
);
System
.
out
.
printf
(
"Parking Simulator\n"
);
int
numberSensors
=
2
*
Runtime
.
getRuntime
().
availableProcessors
();
System
.
out
.
println
(
"Num of Sensors "
+
numberSensors
);
ExecutorService
executorService
=
Executors
.
newFixedThreadPool
(
numberSensors
);
long
start
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
numberSensors
;
i
++)
{
executorService
.
submit
(
new
Sensor
(
stats
));
}
executorService
.
shutdown
();
executorService
.
awaitTermination
(
3
,
TimeUnit
.
SECONDS
);
long
end
=
System
.
currentTimeMillis
()
-
start
;
System
.
out
.
printf
(
"Number of cars: %d\n"
,
stats
.
getNumberCars
());
System
.
out
.
printf
(
"Number of motorcycles: %d\n"
,
stats
.
getNumberMotorcycles
());
cash
.
close
();
System
.
out
.
println
();
System
.
out
.
println
(
"Time Execution: "
+
end
);
}
}
src/main/java/ParkingCash/ParkingCash.java
0 → 100644
View file @
172ae79c
package
ParkingCash
;
public
class
ParkingCash
{
private
static
final
int
cost
=
2
;
private
long
cash
;
public
ParkingCash
()
{
cash
=
0
;
}
public
synchronized
void
vehiclePay
()
{
cash
+=
cost
;
}
public
void
close
()
{
System
.
out
.
printf
(
"Closing accounting"
);
long
totalAmount
;
synchronized
(
this
)
{
totalAmount
=
cash
;
cash
=
0
;
}
System
.
out
.
printf
(
"The total amount is : %d"
,
totalAmount
);
}
}
src/main/java/ParkingCash/ParkingStats.java
0 → 100644
View file @
172ae79c
package
ParkingCash
;
public
class
ParkingStats
{
private
final
Object
controlCars
,
controlMotorcycles
;
private
long
numberCars
;
private
long
numberMotorcycles
;
private
ParkingCash
cash
;
public
ParkingStats
(
ParkingCash
cash
)
{
numberCars
=
0
;
numberMotorcycles
=
0
;
this
.
cash
=
cash
;
this
.
controlCars
=
new
Object
();
this
.
controlMotorcycles
=
new
Object
();
}
public
void
carComeIn
()
{
synchronized
(
controlCars
)
{
numberCars
++;
}
}
public
void
carGoOut
()
{
synchronized
(
controlCars
)
{
numberCars
--;
}
cash
.
vehiclePay
();
}
public
void
motoComeIn
()
{
synchronized
(
controlMotorcycles
)
{
numberMotorcycles
++;
}
}
public
void
motoGoOut
()
{
synchronized
(
controlMotorcycles
)
{
numberMotorcycles
--;
}
cash
.
vehiclePay
();
}
public
long
getNumberCars
()
{
synchronized
(
controlCars
)
{
return
numberCars
;
}
}
public
long
getNumberMotorcycles
()
{
synchronized
(
controlMotorcycles
)
{
return
numberMotorcycles
;
}
}
}
src/main/java/ParkingCash/Sensor.java
0 → 100644
View file @
172ae79c
package
ParkingCash
;
import
java.util.concurrent.TimeUnit
;
public
class
Sensor
implements
Runnable
{
private
ParkingStats
stats
;
public
Sensor
(
ParkingStats
stats
)
{
this
.
stats
=
stats
;
}
@Override
public
void
run
()
{
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
stats
.
carComeIn
();
stats
.
carComeIn
();
try
{
TimeUnit
.
MILLISECONDS
.
sleep
(
50
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
stats
.
motoComeIn
();
try
{
TimeUnit
.
MILLISECONDS
.
sleep
(
50
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
stats
.
motoGoOut
();
stats
.
carGoOut
();
stats
.
carGoOut
();
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment