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
a58a8b2c
Commit
a58a8b2c
authored
Jul 11, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
joiny i demony
parent
95f69a8d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
0 deletions
+127
-0
WatekWypisujacy.java
src/main/java/p28_watki/a_postawy/WatekWypisujacy.java
+28
-0
Watki1.java
src/main/java/p28_watki/a_postawy/Watki1.java
+25
-0
Watki2_Join.java
src/main/java/p28_watki/a_postawy/Watki2_Join.java
+40
-0
Watki3_Demon.java
src/main/java/p28_watki/a_postawy/Watki3_Demon.java
+34
-0
No files found.
src/main/java/p28_watki/a_postawy/WatekWypisujacy.java
0 → 100644
View file @
a58a8b2c
package
p28_watki
.
a_postawy
;
class
WatekWypisujacy
implements
Runnable
{
private
final
String
tekst
;
private
final
int
iloscPowtorzen
;
private
final
int
pauza
;
public
WatekWypisujacy
(
String
tekst
,
int
iloscPowtorzen
,
int
pauza
)
{
this
.
tekst
=
tekst
;
this
.
iloscPowtorzen
=
iloscPowtorzen
;
this
.
pauza
=
pauza
;
}
public
void
run
()
{
System
.
out
.
println
(
tekst
+
" : start wątku, nr "
+
Thread
.
currentThread
().
getId
());
try
{
for
(
int
i
=
1
;
i
<=
iloscPowtorzen
;
i
++)
{
if
(
pauza
>
0
)
{
Thread
.
sleep
(
pauza
);
}
System
.
out
.
println
(
tekst
+
" "
+
i
);
}
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
e
);
}
System
.
out
.
println
(
tekst
+
" : koniec wątku, nr "
+
Thread
.
currentThread
().
getId
());
}
}
src/main/java/p28_watki/a_postawy/Watki1.java
0 → 100644
View file @
a58a8b2c
package
p28_watki
.
a_postawy
;
public
class
Watki1
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Początek main"
);
Thread
th1
=
new
Thread
(
new
WatekWypisujacy
(
"A"
,
100
,
1
));
Thread
th2
=
new
Thread
(
new
WatekWypisujacy
(
"B"
,
100
,
1
));
Thread
th3
=
new
Thread
(
new
WatekWypisujacy
(
"C"
,
100
,
1
));
th1
.
start
();
th2
.
start
();
th3
.
start
();
System
.
out
.
println
(
"main: wątki uruchomione"
);
try
{
Thread
.
sleep
(
50
);
}
catch
(
InterruptedException
e
)
{
}
System
.
out
.
println
(
"main: poczekałem sobie"
);
System
.
out
.
println
(
"koniec main"
);
// main dochodzi do końca, a wątki działają dalej
}
}
src/main/java/p28_watki/a_postawy/Watki2_Join.java
0 → 100644
View file @
a58a8b2c
package
p28_watki
.
a_postawy
;
public
class
Watki2_Join
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Początek main"
);
Thread
th1
=
new
Thread
(
new
WatekWypisujacy
(
"A"
,
100
,
1
));
Thread
th2
=
new
Thread
(
new
WatekWypisujacy
(
"B"
,
100
,
1
));
Thread
th3
=
new
Thread
(
new
WatekWypisujacy
(
"C"
,
100
,
1
));
System
.
out
.
println
(
"stan przed start: "
+
th1
.
getState
());
th1
.
start
();
th2
.
start
();
th3
.
start
();
System
.
out
.
println
(
"main: wątki uruchomione, mój nr "
+
Thread
.
currentThread
().
getId
());
try
{
Thread
.
sleep
(
50
);
}
catch
(
InterruptedException
e
)
{
}
System
.
out
.
println
(
"main: Teraz będę czekał na wątki za pomocą 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
try
{
th1
.
join
();
th2
.
join
();
th3
.
join
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
"main: doczekałem się na zakończenie wątków"
);
System
.
out
.
println
(
"stan po join: "
+
th1
.
getState
());
System
.
out
.
println
(
"koniec main"
);
}
}
src/main/java/p28_watki/a_postawy/Watki3_Demon.java
0 → 100644
View file @
a58a8b2c
package
p28_watki
.
a_postawy
;
public
class
Watki3_Demon
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Początek main"
);
Thread
th1
=
new
Thread
(
new
WatekWypisujacy
(
"A"
,
100
,
1
));
Thread
th2
=
new
Thread
(
new
WatekWypisujacy
(
"B"
,
100
,
1
));
Thread
th3
=
new
Thread
(
new
WatekWypisujacy
(
"C"
,
100
,
1
));
th1
.
setDaemon
(
true
);
th2
.
setDaemon
(
true
);
th3
.
setDaemon
(
true
);
th1
.
start
();
th2
.
start
();
th3
.
start
();
// setDaemon wywołane po start jest niepoprawne - kończy się wyjątkiem
// th1.setDaemon(false);
// th1.setDaemon(true);
System
.
out
.
println
(
"main: wątki uruchomione"
);
try
{
Thread
.
sleep
(
50
);
}
catch
(
InterruptedException
e
)
{
}
System
.
out
.
println
(
"main: poczekałem sobie"
);
System
.
out
.
println
(
"koniec main"
);
// Jeśli w działaniu pozostały wyłącznie wątki będące demonami, to proces jest kończony (i te wątki są "zabijane" w dowolnym momencie).
}
}
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