diff --git a/VL04_09/ThreadPriorityTest/.classpath b/VL04_09/ThreadPriorityTest/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..d171cd4c1231c688762ba221723f4b629bd7667d --- /dev/null +++ b/VL04_09/ThreadPriorityTest/.classpath @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/VL04_09/ThreadPriorityTest/.project b/VL04_09/ThreadPriorityTest/.project new file mode 100644 index 0000000000000000000000000000000000000000..2fb0ab553958c9c329cb94ef772e7d10634310b7 --- /dev/null +++ b/VL04_09/ThreadPriorityTest/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>ThreadPriorityTest</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java b/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java new file mode 100644 index 0000000000000000000000000000000000000000..df4782a589227965cadda9c65004d3eea121b5c7 --- /dev/null +++ b/VL04_09/ThreadPriorityTest/src/inf3/PriorityThread.java @@ -0,0 +1,42 @@ +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; + + PriorityThread(int prio) { + myThreadName = "prio"+prio+"Thread"; + setPriority(prio); + } + + synchronized void addResult() { + //synchronized: das Schreiben der Ergebnisse soll nicht unterbrochen werden + resultStrings.add("thread "+ myThreadName + " ("+ getPriority() + + ") beendet: Operationen: " + count + "; switches: "+ taskSwitches); + } + + 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 + } +} + diff --git a/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java b/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java new file mode 100644 index 0000000000000000000000000000000000000000..28742665702a53cc3d44d3f267d4a2a4ace7d1be --- /dev/null +++ b/VL04_09/ThreadPriorityTest/src/inf3/ThreadPriorityTest.java @@ -0,0 +1,38 @@ +/** 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. +* +* @author Steddin +* @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) { + for(int kk = Thread.MIN_PRIORITY; kk <= Thread.MAX_PRIORITY; kk++) { + priorObjArr[kk-1] = new PriorityThread(kk); + } + 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(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + for (String bla: PriorityThread.resultStrings) { + System.out.println(bla); + } + } +} + + + +