Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
8fed9d0b
Commit
8fed9d0b
authored
Apr 03, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wait/notify i wyplataCzekaj
parent
cfb86bdf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
1 deletions
+60
-1
Konto.java
PC21-Watki/src/konto/Konto.java
+21
-0
Zycie1.java
PC21-Watki/src/konto/Zycie1.java
+1
-1
Zycie2.java
PC21-Watki/src/konto/Zycie2.java
+38
-0
No files found.
PC21-Watki/src/konto/Konto.java
View file @
8fed9d0b
...
@@ -37,8 +37,11 @@ public class Konto {
...
@@ -37,8 +37,11 @@ public class Konto {
throw
new
IllegalArgumentException
(
"Niedodatnia kwota w metodzie wplata"
);
throw
new
IllegalArgumentException
(
"Niedodatnia kwota w metodzie wplata"
);
}
}
saldo
+=
kwota
;
saldo
+=
kwota
;
notify
();
// budzi jeden z czekających wątków
}
}
// W tej wersji metoda w razie braku środków wyrzuca wyjątek
public
synchronized
void
wyplata
(
int
kwota
)
throws
BrakSrodkow
{
public
synchronized
void
wyplata
(
int
kwota
)
throws
BrakSrodkow
{
if
(
kwota
<=
0
)
{
if
(
kwota
<=
0
)
{
throw
new
IllegalArgumentException
(
"Niedodatnia kwota w metodzie wyplata"
);
throw
new
IllegalArgumentException
(
"Niedodatnia kwota w metodzie wyplata"
);
...
@@ -48,4 +51,22 @@ public class Konto {
...
@@ -48,4 +51,22 @@ public class Konto {
}
}
saldo
-=
kwota
;
saldo
-=
kwota
;
}
}
// W tej wersji metoda w razie braku środków czeka aż pieniądze zostaną wpłacone
public
synchronized
void
wyplataCzekaj
(
int
kwota
)
{
if
(
kwota
<=
0
)
{
throw
new
IllegalArgumentException
(
"Niedodatnia kwota w metodzie wyplata"
);
}
try
{
while
(
kwota
>
saldo
)
{
wait
();
}
saldo
-=
kwota
;
notify
();
// budzenie kaskadowe: jeśli dla jednego wątku wystarczyło, to budzony jest następny
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
}
PC21-Watki/src/konto/Zycie1.java
View file @
8fed9d0b
...
@@ -24,7 +24,7 @@ public class Zycie1 {
...
@@ -24,7 +24,7 @@ public class Zycie1 {
Thread
.
sleep
(
700
);
Thread
.
sleep
(
700
);
try
{
try
{
konto
.
wyplata
(
300
);
konto
.
wyplata
(
300
);
System
.
out
.
println
(
"wyplacacz: po wpłacie jest "
+
konto
.
getSaldo
());
System
.
out
.
println
(
"wyplacacz: po w
y
płacie jest "
+
konto
.
getSaldo
());
}
catch
(
BrakSrodkow
e
)
{
}
catch
(
BrakSrodkow
e
)
{
System
.
err
.
println
(
"wyplacacz: "
+
e
.
getMessage
());
System
.
err
.
println
(
"wyplacacz: "
+
e
.
getMessage
());
}
}
...
...
PC21-Watki/src/konto/Zycie2.java
0 → 100644
View file @
8fed9d0b
package
konto
;
public
class
Zycie2
{
public
static
void
main
(
String
[]
args
)
{
Konto
konto
=
new
Konto
(
1
,
"Ala"
,
1400
);
System
.
out
.
println
(
"Stan początkowy: "
+
konto
.
getSaldo
());
Thread
wplacacz
=
new
Thread
(()
->
{
try
{
while
(
true
)
{
Thread
.
sleep
(
5000
);
konto
.
wplata
(
1000
);
System
.
out
.
println
(
"wplacacz: po wpłacie jest "
+
konto
.
getSaldo
());
}
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
});
Thread
wyplacacz
=
new
Thread
(()
->
{
try
{
while
(
true
)
{
Thread
.
sleep
(
700
);
konto
.
wyplataCzekaj
(
300
);
System
.
out
.
println
(
"wyplacacz: po wypłacie jest "
+
konto
.
getSaldo
());
}
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
});
System
.
out
.
println
(
"Startujemy wątki..."
);
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