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

Ueberarbeitung Projekt ThreadPriorityTest

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