Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_mszczonow_1
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
alx_mszczonow_1
Commits
ec882af2
Commit
ec882af2
authored
Jul 11, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wait / notify
parent
db5fca6b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
0 deletions
+82
-0
Konto.java
src/main/java/p28_watki/b_synchro/v1_metody/Konto.java
+16
-0
WyplacanieZOczekiwaniem.java
...28_watki/b_synchro/v1_metody/WyplacanieZOczekiwaniem.java
+66
-0
No files found.
src/main/java/p28_watki/b_synchro/v1_metody/Konto.java
View file @
ec882af2
...
@@ -34,6 +34,7 @@ class Konto {
...
@@ -34,6 +34,7 @@ class Konto {
}
}
saldo
+=
kwota
;
saldo
+=
kwota
;
notify
();
}
}
public
synchronized
void
wyplata
(
int
kwota
)
throws
BrakSrodkow
{
public
synchronized
void
wyplata
(
int
kwota
)
throws
BrakSrodkow
{
...
@@ -47,5 +48,20 @@ class Konto {
...
@@ -47,5 +48,20 @@ class Konto {
saldo
-=
kwota
;
saldo
-=
kwota
;
}
}
public
synchronized
void
wyplataCzekaj
(
int
kwota
)
throws
BrakSrodkow
{
if
(
kwota
<
0
)
{
throw
new
IllegalArgumentException
(
"Ujemna kwota "
+
kwota
+
" w wypłacie"
);
}
try
{
while
(
kwota
>
saldo
)
{
wait
();
}
saldo
-=
kwota
;
notify
();
}
catch
(
InterruptedException
e
)
{
}
}
}
}
src/main/java/p28_watki/b_synchro/v1_metody/WyplacanieZOczekiwaniem.java
0 → 100644
View file @
ec882af2
package
p28_watki
.
b_synchro
.
v1_metody
;
import
java.io.IOException
;
class
WyplacanieZOczekiwaniem
{
static
volatile
boolean
koniec
=
false
;
public
static
void
main
(
String
[]
args
)
{
final
Osoba
ala
=
new
Osoba
(
"Ala"
,
"Kowalska"
,
"2001-01-01"
);
final
Konto
konto
=
new
Konto
(
1
,
1700
,
ala
);
System
.
out
.
println
(
"początek "
+
konto
);
Thread
wplacacz
=
new
Thread
(
new
Runnable
()
{
public
void
run
()
{
while
(!
koniec
)
{
konto
.
wplata
(
1000
);
System
.
out
.
println
(
"wpłacacz: wpłaciłem 1000, saldo = "
+
konto
.
getSaldo
());
try
{
Thread
.
sleep
(
4000
);
}
catch
(
InterruptedException
e
)
{
}
}
}
});
Thread
wyplacacz
=
new
Thread
(
new
Runnable
()
{
public
void
run
()
{
while
(!
koniec
)
{
try
{
konto
.
wyplataCzekaj
(
100
);
}
catch
(
BrakSrodkow
e
)
{
System
.
err
.
println
(
"BRAK ŚRODKÓW"
);
}
System
.
out
.
println
(
"wypłacacz: wypłaciłem 100, saldo = "
+
konto
.
getSaldo
());
try
{
Thread
.
sleep
(
200
);
}
catch
(
InterruptedException
e
)
{
}
}
}
});
wplacacz
.
start
();
wyplacacz
.
start
();
System
.
out
.
println
(
"Wątki wystartowały"
);
System
.
out
.
println
(
"Naciśnij enter aby zakończyć"
);
try
{
System
.
in
.
read
();
}
catch
(
IOException
e1
)
{
}
koniec
=
true
;
try
{
wplacacz
.
join
();
wyplacacz
.
join
();
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
"INTERRUPTED"
);
}
System
.
out
.
println
(
"na końcu: "
+
konto
);
}
}
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