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
4f5e8ae3
Commit
4f5e8ae3
authored
5 years ago
by
Sven Steddin
Browse files
Options
Downloads
Patches
Plain Diff
Ueberarbeitung Projekt ThreadPriorityTest
parent
a3d4539e
No related branches found
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
+19
-27
19 additions, 27 deletions
VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java
VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java
+4
-2
4 additions, 2 deletions
VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java
with
23 additions
and
29 deletions
VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java
+
19
−
27
View file @
4f5e8ae3
...
@@ -2,41 +2,33 @@ package inf3;
...
@@ -2,41 +2,33 @@ package inf3;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
public
class
PriorityThread
extends
Thread
{
public
class
PriorityThread
extends
Thread
{
volatile
long
count
=
0
;
static
ArrayList
<
String
>
resultStrings
=
new
ArrayList
<
String
>();
String
myThreadName
;
volatile
static
long
lastThreadID
=
99999999
;
static
ArrayList
<
String
>
resultStrings
=
new
ArrayList
<
String
>();
volatile
static
boolean
gameOver
=
false
;
volatile
long
result
=
0L
;
static
String
actThreadName
=
"noThread"
;
volatile
long
taskSwitches
=
0
;
volatile
static
boolean
gameOver
=
false
;
volatile
long
count
=
0
;
int
taskSwitches
=
0
;
PriorityThread
(
int
prio
)
{
PriorityThread
(
int
prio
)
{
myThreadName
=
"prio"
+
prio
+
"Thread"
;
setPriority
(
prio
);
setPriority
(
prio
);
lastThreadID
=
Thread
.
currentThread
().
getId
();
}
}
synchronized
void
addResult
()
{
synchronized
void
job
()
{
//synchronized: das Schreiben der Ergebnisse soll nicht unterbrochen werden
long
threadID
=
Thread
.
currentThread
().
getId
();
resultStrings
.
add
(
"thread "
+
myThreadName
+
" ("
+
getPriority
()
+
if
(
lastThreadID
!=
threadID
)
{
") beendet: Operationen: "
+
count
+
"; switches: "
+
taskSwitches
);
lastThreadID
=
threadID
;
taskSwitches
++;
}
result
+=
threadID
;
if
(++
count
>=
1_000_000
)
gameOver
=
true
;
}
}
public
void
run
()
{
public
void
run
()
{
ende:
while
(!
gameOver
)
{
job
();
}
while
(!
gameOver
)
{
resultStrings
.
add
(
"thread prio ("
+
getPriority
()
+
if
(!
actThreadName
.
equals
(
myThreadName
))
{
") beendet:\tOperationen: "
+
count
+
";\tswitches: "
+
actThreadName
=
myThreadName
;
taskSwitches
+
";\t Op/slot: "
+
(
double
)
count
/
taskSwitches
);
taskSwitches
++;
}
for
(
long
ii
=
1
;
ii
<
10
;
ii
++)
{
double
resultat
=
Math
.
log
(
ii
);
if
(
gameOver
)
break
ende
;
}
if
(++
count
>=
10_000_000
)
gameOver
=
true
;
}
addResult
();
//bei der direkten Ausgabe auf System.out wird die Reihenfolge zustzlich
//durch das Threading des System-Streams verflscht; daher eigene Liste erstellt
}
}
}
}
This diff is collapsed.
Click to expand it.
VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java
+
4
−
2
View file @
4f5e8ae3
...
@@ -4,25 +4,27 @@
...
@@ -4,25 +4,27 @@
* zugeordneten Zeitscheiben auswirkt.
* zugeordneten Zeitscheiben auswirkt.
*
*
* @author Steddin
* @author Steddin
* @version 2.00, 2020-01-26 (Steddin) Codeumfang reduziert; Ablauf vereinfacht
* @version 1.00, 2017-01-19
* @version 1.00, 2017-01-19
*/
*/
package
inf3
;
package
inf3
;
public
class
ThreadPriorityTest
{
public
class
ThreadPriorityTest
{
static
PriorityThread
[]
priorObjArr
=
static
PriorityThread
[]
priorObjArr
=
new
PriorityThread
[
Thread
.
MAX_PRIORITY
];
new
PriorityThread
[
Thread
.
MAX_PRIORITY
];
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
//Threads mit unterschiedlichen Prioritten anlegen:
for
(
int
kk
=
Thread
.
MIN_PRIORITY
;
kk
<=
Thread
.
MAX_PRIORITY
;
kk
++)
{
for
(
int
kk
=
Thread
.
MIN_PRIORITY
;
kk
<=
Thread
.
MAX_PRIORITY
;
kk
++)
{
priorObjArr
[
kk
-
1
]
=
new
PriorityThread
(
kk
);
priorObjArr
[
kk
-
1
]
=
new
PriorityThread
(
kk
);
}
}
//Threads gleichzeitig starten:
System
.
out
.
println
(
"Programmstart ... bitte warten"
);
System
.
out
.
println
(
"Programmstart ... bitte warten"
);
for
(
PriorityThread
pth
:
priorObjArr
)
{
for
(
PriorityThread
pth
:
priorObjArr
)
{
pth
.
start
();
pth
.
start
();
}
}
for
(
PriorityThread
pth
:
priorObjArr
)
{
for
(
PriorityThread
pth
:
priorObjArr
)
{
try
{
// ... warten, bis alle Threads beendet sind:
try
{
// ... warten, bis alle Threads beendet sind:
pth
.
join
();
pth
.
join
();
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
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