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

Erweiterung Beispielprojekte

parent 70b1e407
No related branches found
No related tags found
No related merge requests found
<?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>
<?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>
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 zustzlich
//durch das Threading des System-Streams verflscht; daher eigene Liste erstellt
}
}
/** ThreadPriorityTest.java
* Das Programm soll zeigen, wie sich die Zuordnung einer Thread Prioritt
* auf die Hufigkeit der Threadaufrufe und die Lnge 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);
}
}
}
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