Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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
java_dzienna_15_09
Commits
72980bdd
Commit
72980bdd
authored
Sep 15, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pierwsze wątki
parent
34235d88
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
0 deletions
+52
-0
ABC_PierwszeWatki.java
...yJavy/src/main/java/watki/podstawy/ABC_PierwszeWatki.java
+52
-0
No files found.
PC22-ZaawansowaneElementyJavy/src/main/java/watki/podstawy/ABC_PierwszeWatki.java
0 → 100644
View file @
72980bdd
package
watki
.
podstawy
;
public
class
ABC_PierwszeWatki
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Początek main"
);
// Obiekt typu Runnable określa, co ma robić wątek.
// Jest to zapisane w metodzie run tego obiektu.
Runnable
przepis
=
new
Runnable
()
{
public
void
run
()
{
System
.
out
.
println
(
"AAA start"
);
for
(
int
i
=
1
;
i
<=
1000
;
i
++)
{
System
.
out
.
println
(
"AAA "
+
i
);
}
System
.
out
.
println
(
"AAA koniec"
);
}
};
// Sam wątek jest umieszczony w obiekcie typu Thread i najczęściej tworzy się go w oparciu o Runnable.
Thread
watekA
=
new
Thread
(
przepis
);
// Bardzo często obiekt Runable twozy się jako tzw. "klasę anonimową":
Thread
watekB
=
new
Thread
(
new
Runnable
()
{
public
void
run
()
{
System
.
out
.
println
(
"BBB start"
);
for
(
int
i
=
1
;
i
<=
1000
;
i
++)
{
System
.
out
.
println
(
"BBB "
+
i
);
}
System
.
out
.
println
(
"BBB koniec"
);
}
});
// Ale od Java 8 można te użyć wyrażenia lambda.
Thread
watekC
=
new
Thread
(()
->
{
System
.
out
.
println
(
"CCC start"
);
for
(
int
i
=
1
;
i
<=
1000
;
i
++)
{
System
.
out
.
println
(
"CCC "
+
i
);
}
System
.
out
.
println
(
"CCC koniec"
);
});
System
.
out
.
println
(
"Uruchamiam wątki"
);
watekA
.
start
();
watekB
.
start
();
watekC
.
start
();
System
.
out
.
println
(
"Koniec main"
);
}
}
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