Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mtib-Inf3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sven Steddin
mtib-Inf3
Commits
c25cf9cb
Commit
c25cf9cb
authored
5 years ago
by
Sven Steddin
Browse files
Options
Downloads
Patches
Plain Diff
2020-01-26 (Steddin) Programende.java umgebaut
parent
6e7be183
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
VL04_09/ThreadProgrammende/src/inf3/ThreadProgrammende.java
+23
-12
23 additions, 12 deletions
VL04_09/ThreadProgrammende/src/inf3/ThreadProgrammende.java
with
23 additions
and
12 deletions
VL04_09/ThreadProgrammende/src/inf3/ThreadProgrammende.java
+
23
−
12
View file @
c25cf9cb
...
...
@@ -8,22 +8,28 @@ package inf3;
* <li> Es zeigt sich, dass das Programm erst dann zu Ende ist, wenn alle
* User Threads beendet sind. Erst dann wird der Daemon zerstrt. Dies
* geschieht hier nicht bereits mit dem Ende von main().</li>
* <li> Obwohl der Daemon Thread unabhngig von der vorgegebenen Anzahl der
* Thread Durchlufe ist, wird er sptestens abgebrochen, wenn der user
* Thread beendet wird (qed).</li>
* </ul>
*
* @author Steddin
* @version 2.00, 2020-01-26 (Steddin) Programm so umgebaut, dass gezeigt wird,
* wie sich das Programm ohne den Aufruf von sleep(500) verhlt.
* @version 1.01, 2020-01-06
* @version 1.00, 2019-01-18
*/
public
class
ThreadProgrammende
{
/** Hauptprogramm startet einen Daemon-Thread und 2 User-Thread
* @param args Run
n
timeparameter werden nicht gentzt
* @param args Runtimeparameter werden nicht gentzt
* @author Sven Steddin
*/
public
static
boolean
MIT_SLEEP_AUFRUF
=
true
;
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"main-Thread gestartet ..."
);
MyThread
mythread1
=
new
MyThread
(
1
);
MyThread
mythread2
=
new
MyThread
(
2
);
MyThread
mythread1
=
new
MyThread
(
1
,
5
);
MyThread
mythread2
=
new
MyThread
(
2
,
10
);
mythread2
.
setDaemon
(
true
);
mythread1
.
start
();
mythread2
.
start
();
...
...
@@ -36,24 +42,29 @@ public class ThreadProgrammende {
* @author stedS
*/
class
MyThread
extends
Thread
{
int
idx
;
/** Kennnummer des Threads */
int
cnt
=
0
;
/** Anzahl der Aufrufe des Threads */
MyThread
(
int
idx
)
{
int
idx
;
/** Kennnummer des Threads */
int
cnt
;
/** Anzahl der Aufrufe des Threads */
int
maxCnt
;
/** max. Anzahl der Durchlufe des Threads */
MyThread
(
int
idx
,
int
maxCnt
)
{
super
();
cnt
=
0
;
this
.
idx
=
idx
;
this
.
maxCnt
=
maxCnt
;
}
public
void
run
()
{
while
(
true
)
{
String
t
=
this
.
isDaemon
()
?
"Daemon"
:
"Normal"
;
System
.
out
.
println
(
"Thread "
+
idx
+
" ["
+
t
+
"] : "
+
++
cnt
+
". Durchlauf"
);
if
(!
this
.
isDaemon
()
&&
(
cnt
>
10
))
{
if
(!
this
.
isDaemon
()
&&
(
cnt
>=
maxCnt
))
{
System
.
out
.
println
(
"Thread "
+
idx
+
" ["
+
t
+
"] : Abbruch"
);
System
.
out
.
println
(
"Luft der Daemon weiter?"
);
break
;
}
try
{
sleep
(
600
);
// was passiert, wenn sleep() nicht aufgerufen wird?
// ... lsst sich nicht direkt vorhersagen --> ausprobieren!
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
if
(
ThreadProgrammende
.
MIT_SLEEP_AUFRUF
)
{
try
{
sleep
(
500
);
// was passiert, wenn sleep() nicht aufgerufen wird?
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment