Skip to content
Snippets Groups Projects
Commit 4b36f697 authored by Sven Steddin's avatar Sven Steddin
Browse files

ThreadPriorityTest Code verbessert

parent 4f5e8ae3
No related branches found
No related tags found
No related merge requests found
...@@ -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 >= 10_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?
}
}
} }
/** 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());
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment