Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_20251009
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_20251009
Commits
0245b51d
Commit
0245b51d
authored
Oct 09, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Coś na temat puli wątków.
parent
bf783d1a
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
121 additions
and
2 deletions
+121
-2
PuleWatkow1.java
src/main/java/p04_watki/dodatkowe/PuleWatkow1.java
+44
-0
PuleWatkow2.java
src/main/java/p04_watki/dodatkowe/PuleWatkow2.java
+71
-0
ConcMap.java
src/main/java/p04_watki/kolekcje/ConcMap.java
+2
-0
CustomThreadPool.java
src/main/java/p04_watki/pule/CustomThreadPool.java
+2
-0
PuleWatkow.java
src/main/java/p04_watki/pule/PuleWatkow.java
+2
-2
No files found.
src/main/java/p04_watki/dodatkowe/PuleWatkow1.java
0 → 100644
View file @
0245b51d
package
p04_watki
.
dodatkowe
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
public
class
PuleWatkow1
{
// Wiele zadań typu Runnable i jedno typu Callable
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Początek main"
);
Future
<
String
>
future
=
null
;
try
(
ExecutorService
executorService
=
Executors
.
newFixedThreadPool
(
4
))
{
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
var
numer
=
i
;
executorService
.
submit
(()
->
{
System
.
out
.
println
(
"Runnable "
+
numer
+
" , wątek "
+
Thread
.
currentThread
().
getName
());
try
{
Thread
.
sleep
(
500
);
}
catch
(
InterruptedException
e
)
{
}
});
}
future
=
executorService
.
submit
(()
->
"wynik pierwszego callable z wątku "
+
Thread
.
currentThread
().
getName
());
System
.
out
.
println
(
"Wsystkie zadania zlecone."
);
System
.
out
.
println
(
"Stan obiektu Future: "
+
future
.
isDone
());
System
.
out
.
println
(
future
);
}
System
.
out
.
println
(
"Pula zamknięta"
);
System
.
out
.
println
(
"Teraz odczytuję wynik z Future"
);
try
{
System
.
out
.
println
(
future
.
get
());
}
catch
(
InterruptedException
|
ExecutionException
e
)
{
throw
new
RuntimeException
(
e
);
}
System
.
out
.
println
(
"Koniec main"
);
}
}
src/main/java/p04_watki/dodatkowe/PuleWatkow2.java
0 → 100644
View file @
0245b51d
package
p04_watki
.
dodatkowe
;
import
java.time.Duration
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.concurrent.*
;
public
class
PuleWatkow2
{
// W pętli zlecamy wiele zadań typu Callable i zwracane obiekty Future dodajemy do listy.
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Początek main"
);
try
(
ExecutorService
executorService
=
Executors
.
newFixedThreadPool
(
4
))
{
// synchronizowana nie jest tu konieczna, bo tylko w jednym wątku używamy tej listy
// List<Future<String>> futures = Collections.synchronizedList(new ArrayList<>());
List
<
Future
<
String
>>
futures
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
20
;
i
++)
{
var
numer
=
i
;
Future
<
String
>
future
=
executorService
.
submit
(()
->
{
System
.
out
.
println
(
"Wątek "
+
Thread
.
currentThread
().
getName
()
+
" wykonuje task nr "
+
numer
);
int
czas
=
ThreadLocalRandom
.
current
().
nextInt
(
300
,
1300
);
try
{
Thread
.
sleep
(
Duration
.
ofMillis
(
czas
));
}
catch
(
InterruptedException
e
)
{
}
return
"Wynik zadania nr "
+
numer
;
});
futures
.
add
(
future
);
}
System
.
out
.
println
(
"Wszystkie zadanie zlecone"
);
// Dopóki lista zawiera jakiekolwiek zadania, przeglądamy ją w poszukiwaniu zadań z isDome == true
// i wyniki tych zadań odbieramy w pierwszej kolejności
// to by tylko usunęło bez odczytywania:
// futures.removeIf(Future::isDone);
while
(!
futures
.
isEmpty
())
{
boolean
cokolwiek
=
false
;
for
(
Iterator
<
Future
<
String
>>
it
=
futures
.
iterator
();
it
.
hasNext
();
)
{
Future
<
String
>
future
=
it
.
next
();
if
(
future
.
isDone
())
{
cokolwiek
=
true
;
it
.
remove
();
try
{
String
wynik
=
future
.
get
();
System
.
out
.
println
(
"Wynik: "
+
wynik
);
}
catch
(
InterruptedException
|
ExecutionException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
if
(!
cokolwiek
)
{
System
.
out
.
println
(
"Nie ma żadnego zakończonego zadania"
);
try
{
Thread
.
sleep
(
Duration
.
ofMillis
(
200
));
}
catch
(
InterruptedException
e
)
{
}
}
}
}
System
.
out
.
println
(
"Pula zamknięta"
);
System
.
out
.
println
(
"Koniec main"
);
}
}
src/main/java/p04_watki/kolekcje/ConcMap.java
View file @
0245b51d
package
p04_watki
.
kolekcje
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ExecutorService
;
...
...
src/main/java/p04_watki/pule/CustomThreadPool.java
View file @
0245b51d
...
...
@@ -11,6 +11,8 @@ public class CustomThreadPool {
new
ThreadPoolExecutor
.
CallerRunsPolicy
()
// Zadania są wykonywane przez wątek wywołujący w razie braku miejsca w tej puli
);
// executor.prestartAllCoreThreads();
executor
.
submit
(()
->
System
.
out
.
println
(
"Task executed"
));
executor
.
shutdown
();
}
...
...
src/main/java/p04_watki/pule/PuleWatkow.java
View file @
0245b51d
...
...
@@ -8,8 +8,8 @@ public class PuleWatkow {
final
int
N
=
100
;
// liczba procedur do wykonania
// ExecutorService pool = Executors.newSingleThreadExecutor();
ExecutorService
pool
=
Executors
.
newFixedThreadPool
(
20
);
//
ExecutorService pool = Executors.newCachedThreadPool(); // tworzy wątek, gdy tylko brakuje robotników
//
ExecutorService pool = Executors.newFixedThreadPool(20);
ExecutorService
pool
=
Executors
.
newCachedThreadPool
();
// tworzy wątek, gdy tylko brakuje robotników
// 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ść
...
...
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