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
4b36f697
Commit
4b36f697
authored
5 years ago
by
Sven Steddin
Browse files
Options
Downloads
Patches
Plain Diff
ThreadPriorityTest Code verbessert
parent
4f5e8ae3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java
+11
-6
11 additions, 6 deletions
VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java
VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java
+10
-4
10 additions, 4 deletions
VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java
with
21 additions
and
10 deletions
VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java
+
11
−
6
View file @
4b36f697
...
@@ -2,7 +2,6 @@ package inf3;
...
@@ -2,7 +2,6 @@ package inf3;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
public
class
PriorityThread
extends
Thread
{
public
class
PriorityThread
extends
Thread
{
static
ArrayList
<
String
>
resultStrings
=
new
ArrayList
<
String
>();
volatile
static
long
lastThreadID
=
99999999
;
volatile
static
long
lastThreadID
=
99999999
;
volatile
static
boolean
gameOver
=
false
;
volatile
static
boolean
gameOver
=
false
;
volatile
long
result
=
0L
;
volatile
long
result
=
0L
;
...
@@ -14,21 +13,27 @@ public class PriorityThread extends Thread {
...
@@ -14,21 +13,27 @@ public class PriorityThread extends Thread {
lastThreadID
=
Thread
.
currentThread
().
getId
();
lastThreadID
=
Thread
.
currentThread
().
getId
();
}
}
synchronized
void
job
()
{
void
job
()
{
long
threadID
=
Thread
.
currentThread
().
getId
();
long
threadID
=
Thread
.
currentThread
().
getId
();
if
(
lastThreadID
!=
threadID
)
{
if
(
lastThreadID
!=
threadID
)
{
lastThreadID
=
threadID
;
lastThreadID
=
threadID
;
taskSwitches
++;
taskSwitches
++;
}
}
result
+=
threadID
;
result
+=
threadID
;
if
(++
count
>=
1_000_000
)
gameOver
=
true
;
if
(++
count
>=
1
0
_000_000
)
gameOver
=
true
;
}
}
public
void
run
()
{
String
getResultStr
()
{
while
(!
gameOver
)
{
job
();
}
return
(
"thread prio ("
+
getPriority
()
+
resultStrings
.
add
(
"thread prio ("
+
getPriority
()
+
") beendet:\tOperationen: "
+
count
+
";\tswitches: "
+
") beendet:\tOperationen: "
+
count
+
";\tswitches: "
+
taskSwitches
+
";\t Op/slot: "
+
(
double
)
count
/
taskSwitches
);
taskSwitches
+
";\t Op/slot: "
+
(
double
)
count
/
taskSwitches
);
}
}
public
void
run
()
{
while
(!
gameOver
)
{
job
();
//Thread.yield(); //was bewirkt der Aufruf von yield() an dieser Stelle?
}
}
}
}
This diff is collapsed.
Click to expand it.
VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java
+
10
−
4
View file @
4b36f697
/** ThreadPriorityTest.java
/** ThreadPriorityTest.java
* Das Programm soll zeigen, wie sich die Zuordnung einer Thread Priorität
* Das Programm soll zeigen, wie sich die Zuordnung einer Thread Priorität
* auf die Häufigkeit der Threadaufrufe und die Länge der jedem Thread
* auf die Häufigkeit der Threadaufrufe und die Länge der jedem Thread
* zugeordneten Zeitscheiben auswirkt.
* zugeordneten Zeitscheiben auswirkt.
*
* Der Vergleich zwischen Ausführung des Programms mit und ohne yield()-Befehl
* verdeutlicht die Strategie der JVM bei der Berücksichtigung untersch. Prioritäten.
*
*
* @author Steddin
* @author Steddin
* @version 2.00, 2020-01-26 (Steddin) Codeumfang reduziert; Ablauf vereinfacht
* @version 2.00, 2020-01-26 (Steddin) Codeumfang reduziert; Ablauf vereinfacht
...
@@ -28,9 +31,12 @@ public class ThreadPriorityTest {
...
@@ -28,9 +31,12 @@ public class ThreadPriorityTest {
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
}
}
for
(
String
bla:
PriorityThread
.
resultStrings
)
{
//Auch nach Durchlauf der run() Methode ist das Thread-Object noch verfügbar und
System
.
out
.
println
(
bla
);
//es kann auf seine Daten zugegriffen weerden: Ausgabe der Ergebnisse erst nach
//Abschluss aller Threads, um die Ausführung der Threads nicht zu stören
for
(
int
kk
=
0
;
kk
<
Thread
.
MAX_PRIORITY
;
kk
++)
{
System
.
out
.
println
(
priorObjArr
[
kk
].
getResultStr
());
}
}
}
}
}
}
...
...
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