diff --git a/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java b/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java index 378e0a0361bd1837610cce5f4471e8b6336485c7..a0424ec0432147fbf23e2ae397b37495f2da2bb7 100644 --- a/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java +++ b/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java @@ -2,7 +2,6 @@ package inf3; import java.util.ArrayList; public class PriorityThread extends Thread { - static ArrayList <String> resultStrings = new ArrayList<String>(); volatile static long lastThreadID = 99999999; volatile static boolean gameOver = false; volatile long result = 0L; @@ -14,21 +13,27 @@ public class PriorityThread extends Thread { lastThreadID = Thread.currentThread().getId(); } - synchronized void job() { + void job() { long threadID = Thread.currentThread().getId(); if (lastThreadID != threadID) { lastThreadID = threadID; taskSwitches++; } result += threadID; - if (++count >= 1_000_000) gameOver = true; + if (++count >= 10_000_000) gameOver = true; } - public void run() { - while (!gameOver) { job(); } - resultStrings.add("thread prio ("+ getPriority() + + String getResultStr() { + return ("thread prio ("+ getPriority() + ") beendet:\tOperationen: " + count + ";\tswitches: "+ taskSwitches+";\t Op/slot: "+ (double)count/taskSwitches); } + + public void run() { + while (!gameOver) { + job(); + //Thread.yield(); //was bewirkt der Aufruf von yield() an dieser Stelle? + } + } } diff --git a/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java b/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java index efe55d44a10c2a65889d58230e7cb55b3ae8860e..560b4a5cf546a25494886e53326ca94e809c5d60 100644 --- a/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java +++ b/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java @@ -1,7 +1,10 @@ /** ThreadPriorityTest.java * 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 -* 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 * @version 2.00, 2020-01-26 (Steddin) Codeumfang reduziert; Ablauf vereinfacht @@ -28,9 +31,12 @@ public class ThreadPriorityTest { } catch (InterruptedException e) { e.printStackTrace(); } - } - for (String bla: PriorityThread.resultStrings) { - System.out.println(bla); + } + //Auch nach Durchlauf der run() Methode ist das Thread-Object noch verfügbar und + //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()); } } }