diff --git a/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java b/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java index df4782a589227965cadda9c65004d3eea121b5c7..378e0a0361bd1837610cce5f4471e8b6336485c7 100644 --- a/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java +++ b/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java @@ -2,41 +2,33 @@ package inf3; import java.util.ArrayList; public class PriorityThread extends Thread { - volatile long count = 0; - String myThreadName; - static ArrayList <String> resultStrings = new ArrayList<String>(); - - static String actThreadName = "noThread"; - volatile static boolean gameOver = false; - int taskSwitches = 0; + static ArrayList <String> resultStrings = new ArrayList<String>(); + volatile static long lastThreadID = 99999999; + volatile static boolean gameOver = false; + volatile long result = 0L; + volatile long taskSwitches = 0; + volatile long count = 0; PriorityThread(int prio) { - myThreadName = "prio"+prio+"Thread"; setPriority(prio); + lastThreadID = Thread.currentThread().getId(); } - synchronized void addResult() { - //synchronized: das Schreiben der Ergebnisse soll nicht unterbrochen werden - resultStrings.add("thread "+ myThreadName + " ("+ getPriority() + - ") beendet: Operationen: " + count + "; switches: "+ taskSwitches); + synchronized void job() { + long threadID = Thread.currentThread().getId(); + if (lastThreadID != threadID) { + lastThreadID = threadID; + taskSwitches++; + } + result += threadID; + if (++count >= 1_000_000) gameOver = true; } public void run() { - ende: - while (!gameOver) { - if (!actThreadName.equals(myThreadName)) { - actThreadName = myThreadName; - 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 zusätzlich - //durch das Threading des System-Streams verfälscht; daher eigene Liste erstellt + while (!gameOver) { job(); } + resultStrings.add("thread prio ("+ getPriority() + + ") beendet:\tOperationen: " + count + ";\tswitches: "+ + taskSwitches+";\t Op/slot: "+ (double)count/taskSwitches); } } diff --git a/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java b/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java index 28742665702a53cc3d44d3f267d4a2a4ace7d1be..efe55d44a10c2a65889d58230e7cb55b3ae8860e 100644 --- a/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java +++ b/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java @@ -4,25 +4,27 @@ * zugeordneten Zeitscheiben auswirkt. * * @author Steddin +* @version 2.00, 2020-01-26 (Steddin) Codeumfang reduziert; Ablauf vereinfacht * @version 1.00, 2017-01-19 */ package inf3; public class ThreadPriorityTest { - static PriorityThread [] priorObjArr = new PriorityThread[Thread.MAX_PRIORITY]; public static void main(String[] args) { + //Threads mit unterschiedlichen Prioritäten anlegen: for(int kk = Thread.MIN_PRIORITY; kk <= Thread.MAX_PRIORITY; kk++) { priorObjArr[kk-1] = new PriorityThread(kk); } + //Threads gleichzeitig starten: System.out.println("Programmstart ... bitte warten"); for (PriorityThread pth : priorObjArr) { pth.start(); } for (PriorityThread pth : priorObjArr) { try { // ... warten, bis alle Threads beendet sind: - pth.join(); + pth.join(); } catch (InterruptedException e) { e.printStackTrace(); }