From 4f5e8ae300e655772a08b3c4cc8a237eb6457b93 Mon Sep 17 00:00:00 2001 From: stedS <sven.steddin@reutlingen-university.de> Date: Mon, 27 Jan 2020 00:18:47 +0100 Subject: [PATCH] Ueberarbeitung Projekt ThreadPriorityTest --- .../src/inf3/PriorityThread.java | 46 ++++++++----------- .../src/inf3/ThreadPriorityTest.java | 6 ++- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java b/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java index df4782a..378e0a0 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 2874266..efe55d4 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(); } -- GitLab