Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
Overview
Overview
Details
Activity
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
Patryk Czarnik
java_dzienna_15_09
Commits
6e0c3d39
Commit
6e0c3d39
authored
Sep 15, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wait i notify
parent
0a400444
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
0 deletions
+52
-0
Konto.java
...ansowaneElementyJavy/src/main/java/watki/konto/Konto.java
+15
-0
WyplacanieZCzekaniem.java
...yJavy/src/main/java/watki/konto/WyplacanieZCzekaniem.java
+37
-0
No files found.
PC22-ZaawansowaneElementyJavy/src/main/java/watki/konto/Konto.java
View file @
6e0c3d39
...
...
@@ -54,4 +54,19 @@ public class Konto {
}
saldo
-=
kwota
;
}
public
synchronized
void
wyplataCzekaj
(
int
kwota
)
throws
BrakSrodkow
{
if
(
kwota
<=
0
)
{
throw
new
IllegalArgumentException
(
"Ujemna kwota w wyplata"
);
}
try
{
while
(
kwota
>
saldo
)
{
wait
();
}
saldo
-=
kwota
;
notify
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC22-ZaawansowaneElementyJavy/src/main/java/watki/konto/WyplacanieZCzekaniem.java
0 → 100644
View file @
6e0c3d39
package
watki
.
konto
;
public
class
WyplacanieZCzekaniem
{
public
static
void
main
(
String
[]
args
)
{
Konto
konto
=
new
Konto
(
1
,
"Ala"
,
2200
);
System
.
out
.
println
(
"Stan początkowy: "
+
konto
.
getSaldo
());
Thread
wplacacz
=
new
Thread
(()
->
{
while
(
true
)
{
try
{
Thread
.
sleep
(
2000
);
konto
.
wplata
(
2000
);
System
.
out
.
println
(
"wpłaciłem, teraz jest "
+
konto
.
getSaldo
());
}
catch
(
InterruptedException
e
)
{
}
}
});
Thread
wyplacacz
=
new
Thread
(()
->
{
while
(
true
)
{
try
{
Thread
.
sleep
(
200
);
System
.
out
.
println
(
"próbuję wypłacić"
);
konto
.
wyplataCzekaj
(
300
);
System
.
out
.
println
(
"wypłaciłem, teraz jest "
+
konto
.
getSaldo
());
}
catch
(
BrakSrodkow
e
)
{
System
.
err
.
println
(
"BRAK KASY"
);
}
catch
(
InterruptedException
e
)
{
}
}
});
wplacacz
.
start
();
wyplacacz
.
start
();
}
}
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