Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jvstd1
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
jvstd1
Commits
2caeda77
Commit
2caeda77
authored
Oct 23, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Konto w wątkach - wypłacanie z czekaniem
parent
95a3dfa0
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
0 deletions
+51
-0
Konto.java
src/main/java/p19_watki/konto/v1/Konto.java
+16
-0
Wyplacanie2.java
src/main/java/p19_watki/konto/v1/Wyplacanie2.java
+35
-0
No files found.
src/main/java/p19_watki/konto/v1/Konto.java
View file @
2caeda77
...
@@ -46,6 +46,7 @@ public class Konto {
...
@@ -46,6 +46,7 @@ public class Konto {
throw
new
IllegalArgumentException
(
"Kwota wpłaty nie jest dodatnia"
);
throw
new
IllegalArgumentException
(
"Kwota wpłaty nie jest dodatnia"
);
}
}
saldo
+=
kwota
;
saldo
+=
kwota
;
notify
();
}
}
public
synchronized
void
wyplata
(
int
kwota
)
throws
BrakSrodkow
{
public
synchronized
void
wyplata
(
int
kwota
)
throws
BrakSrodkow
{
...
@@ -58,6 +59,20 @@ public class Konto {
...
@@ -58,6 +59,20 @@ public class Konto {
saldo
-=
kwota
;
saldo
-=
kwota
;
}
}
public
synchronized
void
wyplataCzekaj
(
int
kwota
)
throws
BrakSrodkow
{
if
(
kwota
<=
0
)
{
throw
new
IllegalArgumentException
(
"Kwota wypłaty nie jest dodatnia"
);
}
try
{
while
(
kwota
>
saldo
)
{
wait
();
}
saldo
-=
kwota
;
notify
();
}
catch
(
InterruptedException
e
)
{
}
}
public
synchronized
void
przelew
(
Konto
cel
,
int
kwota
)
throws
BrakSrodkow
{
public
synchronized
void
przelew
(
Konto
cel
,
int
kwota
)
throws
BrakSrodkow
{
if
(
kwota
<=
0
)
{
if
(
kwota
<=
0
)
{
throw
new
IllegalArgumentException
(
"Kwota przelewu nie jest dodatnia"
);
throw
new
IllegalArgumentException
(
"Kwota przelewu nie jest dodatnia"
);
...
@@ -70,6 +85,7 @@ public class Konto {
...
@@ -70,6 +85,7 @@ public class Konto {
// gdyby dwa wątki w tym samym czasie chciały robić przelew z A na B i z B na A
// gdyby dwa wątki w tym samym czasie chciały robić przelew z A na B i z B na A
synchronized
(
cel
)
{
synchronized
(
cel
)
{
cel
.
saldo
+=
kwota
;
cel
.
saldo
+=
kwota
;
cel
.
notify
();
}
}
}
}
}
}
src/main/java/p19_watki/konto/v1/Wyplacanie2.java
0 → 100644
View file @
2caeda77
package
p19_watki
.
konto
.
v1
;
public
class
Wyplacanie2
{
public
static
void
main
(
String
[]
args
)
{
Konto
konto
=
new
Konto
(
1
,
"Ala"
,
15_000
);
System
.
out
.
println
(
"początek main: "
+
konto
);
Thread
wplacacz
=
new
Thread
(()
->
{
try
{
while
(
true
)
{
Thread
.
sleep
(
10_000
);
konto
.
wplata
(
10_000
);
System
.
out
.
println
(
"Po wpłacie jest "
+
konto
.
getSaldo
());
}
}
catch
(
InterruptedException
e
)
{}
});
Thread
wyplacacz
=
new
Thread
(()
->
{
try
{
while
(
true
)
{
try
{
Thread
.
sleep
(
500
);
konto
.
wyplataCzekaj
(
700
);
System
.
out
.
println
(
"Po wypłacie jest "
+
konto
.
getSaldo
());
}
catch
(
BrakSrodkow
e
)
{
System
.
err
.
println
(
e
.
getMessage
());
}
}
}
catch
(
InterruptedException
e
)
{}
});
wplacacz
.
start
();
wyplacacz
.
start
();
System
.
out
.
println
(
"wątki uruchomione"
);
}
}
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