Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20250331
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
20250331
Commits
13bb6d93
Commit
13bb6d93
authored
Apr 01, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
watki zmiany
parent
f2df6e89
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
50 additions
and
16 deletions
+50
-16
Afrykarium3.java
...p50_watki/ilustracje_nie_do_uruchamiania/Afrykarium3.java
+1
-1
ConcMap.java
src/main/java/gotowe/p50_watki/kolekcje/ConcMap.java
+3
-3
ProdKons1.java
src/main/java/gotowe/p50_watki/kolekcje/ProdKons1.java
+0
-1
Konto.java
...gotowe/p50_watki/konta/v2_synchronizacja_metod/Konto.java
+1
-1
Konto.java
src/main/java/gotowe/p50_watki/konta/v4_locki/Konto.java
+0
-1
Przerwanie.java
src/main/java/gotowe/p50_watki/podstawy/Przerwanie.java
+0
-1
Watki2_Join.java
src/main/java/gotowe/p50_watki/podstawy/Watki2_Join.java
+1
-1
CustomThreadPool.java
src/main/java/gotowe/p50_watki/pule/CustomThreadPool.java
+17
-0
PuleWatkow.java
src/main/java/gotowe/p50_watki/pule/PuleWatkow.java
+3
-7
PuleWatkowCallable.java
src/main/java/gotowe/p50_watki/pule/PuleWatkowCallable.java
+1
-0
PuleWatkow_Lambda.java
src/main/java/gotowe/p50_watki/pule/PuleWatkow_Lambda.java
+23
-0
No files found.
src/main/java/gotowe/p50_watki/ilustracje_nie_do_uruchamiania/Afrykarium3.java
View file @
13bb6d93
...
...
@@ -8,7 +8,7 @@ public class Afrykarium3 {
public
void
wpu
ść
Wycieczk
ę
(
int
ilu
)
{
try
{
// zmniejsza wartość
semafora o ilu,
// zmniejsza wartość
semafora o ilu,
// ale jeśli wartość semafora < ilu, to czeka, aż semafor uzyska odp. wartość.
miejsca
.
acquire
(
ilu
);
// -= akademicko : P
}
catch
(
InterruptedException
e
)
{
...
...
src/main/java/gotowe/p50_watki/kolekcje/ConcMap.java
View file @
13bb6d93
...
...
@@ -29,10 +29,10 @@ public class ConcMap {
private
static
final
int
R
=
10_000
;
// odkomentuj jedną z wersji i sprawdź
//private final Map<String, Integer> map = Collections.synchronizedMap(new HashMap<>());
// private final Map<String, Integer> map = new HashMap<>(); // uwaga - to będzie dawać błędy "nadpisanych danych"
// private final Map<String, Integer> map = Collections.synchronizedMap(new HashMap<>());
private
final
Map
<
String
,
Integer
>
map
=
new
ConcurrentHashMap
<>();
//private final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>();
// private final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>();
private
void
dzialaj
()
{
final
ThreadLocalRandom
random
=
ThreadLocalRandom
.
current
();
...
...
src/main/java/gotowe/p50_watki/kolekcje/ProdKons1.java
View file @
13bb6d93
...
...
@@ -48,7 +48,6 @@ public class ProdKons1 {
}
catch
(
InterruptedException
e
)
{
}
}
}
}
...
...
src/main/java/gotowe/p50_watki/konta/v2_synchronizacja_metod/Konto.java
View file @
13bb6d93
...
...
@@ -47,7 +47,7 @@ class Konto {
throw
new
IllegalArgumentException
(
"Ujemna kwota "
+
kwota
+
" w wypłacie"
);
}
if
(
kwota
>
saldo
)
{
throw
new
BrakSrodkow
(
"Brak
środków na koncie nr "
+
numer
);
throw
new
BrakSrodkow
(
"Brak
środków na koncie nr "
+
numer
);
}
saldo
-=
kwota
;
...
...
src/main/java/gotowe/p50_watki/konta/v4_locki/Konto.java
View file @
13bb6d93
...
...
@@ -77,7 +77,6 @@ class Konto {
if
(
kwota
>
saldo
)
{
throw
new
BrakSrodkow
(
"Brak środków na koncie nr "
+
numer
);
}
saldo
-=
kwota
;
}
finally
{
lock
.
unlock
();
...
...
src/main/java/gotowe/p50_watki/podstawy/Przerwanie.java
View file @
13bb6d93
...
...
@@ -29,7 +29,6 @@ public class Przerwanie {
System
.
in
.
read
();
// czekamy na ENTER i gdy zostanie naciśnięty, wywołamy interrupt na wątku
th
.
interrupt
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
};
...
...
src/main/java/gotowe/p50_watki/podstawy/Watki2_Join.java
View file @
13bb6d93
...
...
@@ -23,7 +23,7 @@ public class Watki2_Join {
System
.
out
.
println
(
"stan przed join: "
+
th1
.
getState
());
// wątek main czeka na zakończenie wątków th1, th2, th3
// jeśli one się
skończyły wcześniej, to od razu przechodzi dalej
// jeśli one się
skończyły wcześniej, to od razu przechodzi dalej
try
{
th1
.
join
();
th2
.
join
();
...
...
src/main/java/gotowe/p50_watki/pule/CustomThreadPool.java
0 → 100644
View file @
13bb6d93
package
gotowe
.
p50_watki
.
pule
;
import
java.util.concurrent.*
;
public
class
CustomThreadPool
{
public
static
void
main
(
String
[]
args
)
{
ThreadPoolExecutor
executor
=
new
ThreadPoolExecutor
(
2
,
4
,
60
,
TimeUnit
.
SECONDS
,
new
ArrayBlockingQueue
<>(
10
),
// Ograniczona kolejka
new
ThreadPoolExecutor
.
CallerRunsPolicy
()
// Zadania są wykonywane przez wątek wywołujący w razie braku miejsca w tej puli
);
executor
.
submit
(()
->
System
.
out
.
println
(
"Task executed"
));
executor
.
shutdown
();
}
}
src/main/java/gotowe/p50_watki/pule/PuleWatkow.java
View file @
13bb6d93
package
gotowe
.
p50_watki
.
pule
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.*
;
public
class
PuleWatkow
{
...
...
@@ -17,7 +13,7 @@ public class PuleWatkow {
// ExecutorService pool = Executors.newWorkStealingPool(); // od Javy 8 - stara się wykorzystać wszystkie procesory
// ExecutorService pool = Executors.newWorkStealingPool(2); // wersja z ograniczeniem współbieżności do pewnego poziomu
// ScheduledExecutorService pool = Executors.newScheduledThreadPool(20); // pozwala planować zadania na przyszłość
Procedura
zadanie
=
new
Procedura
();
System
.
out
.
println
(
"Zaczynam zlecać..."
);
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
...
...
@@ -33,7 +29,7 @@ public class PuleWatkow {
System
.
out
.
println
(
"Zlecilem wykonanie"
);
pool
.
shutdown
();
//pool.shutdownNow();
//
pool.shutdownNow();
System
.
out
.
println
(
"Po shutdown"
);
//pool.submit(() -> {});
try
{
...
...
src/main/java/gotowe/p50_watki/pule/PuleWatkowCallable.java
View file @
13bb6d93
...
...
@@ -29,6 +29,7 @@ public class PuleWatkowCallable {
pool
.
shutdown
();
System
.
out
.
println
(
"Po shutdown"
);
System
.
out
.
println
(
gfuture
.
isDone
());
// gfuture.cancel(false);
String
wynik
=
gfuture
.
get
();
// to powoduje oczekiwanie na zakończenie zadania
System
.
out
.
println
(
gfuture
.
isDone
()
+
wynik
);
//pool.shutdownNow();
...
...
src/main/java/gotowe/p50_watki/pule/PuleWatkow_Lambda.java
0 → 100644
View file @
13bb6d93
package
gotowe
.
p50_watki
.
pule
;
import
java.util.concurrent.*
;
public
class
PuleWatkow_Lambda
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
,
ExecutionException
{
try
(
ExecutorService
pool
=
Executors
.
newFixedThreadPool
(
4
))
{
Future
<?>
futureVoid
=
pool
.
submit
(()
->
{
System
.
out
.
println
(
"Zadanie Runnable"
);
});
Future
<
String
>
futureString
=
pool
.
submit
(()
->
{
System
.
out
.
println
(
"Zadanie Callable"
);
return
"to jest wynik"
;
});
while
(!
futureString
.
isDone
())
{
System
.
out
.
println
(
"czekam na koniec String"
);
}
String
s
=
futureString
.
get
();
System
.
out
.
println
(
"gotowe: "
+
s
);
}
}
}
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