Thread.currentThread().setPriority(Thread.NORM_PRIORITY); if (supportsThreadPriorities()) { if (haveNicenessApis()) { // Should have no impact, because it's not inherited.
VMRuntime.getRuntime().setThreadNiceness(Thread.currentThread(), 10);
}
PriorityStoringThread t1 = new PriorityStoringThread(false);
t1.setPriority(Thread.MAX_PRIORITY);
t1.start(); if (t1.getPriority() != Thread.MAX_PRIORITY) {
System.out.print("thread priority for t1 while running was " + t1.getPriority() + " [expected Thread.MAX_PRIORITY]\n");
System.out.println(getPriorityInfo());
}
t1.join(); if (t1.getPriority() != Thread.MAX_PRIORITY) {
System.out.print("thread priority for t1 while running was " + t1.getPriority() + " [expected Thread.MAX_PRIORITY]\n");
System.out.println(getPriorityInfo());
} if (t1.nativePriority != Thread.MAX_PRIORITY) {
System.out.print("thread priority for t1 was " + t1.nativePriority + " [expected Thread.MAX_PRIORITY]\n");
System.out.println(getPriorityInfo());
} if (t1.reportedPriority != Thread.MAX_PRIORITY) {
System.out.println("Reported Java priority is wrong");
} if (t1.reportedNiceness != NICENESS_UNKNOWN && t1.reportedNiceness >= 0) { // Niceness should correspond to MAX_PRIORITY.
System.out.println("Reported Java niceness is wrong");
}
// Repeat, raising parent priority instead of child priority. // Results should be the same. Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
PriorityStoringThread t2 = new PriorityStoringThread(false);
t2.start(); if (t2.getPriority() != Thread.MAX_PRIORITY) {
System.out.print("thread priority for t2 while running was " + t2.getPriority() + " [expected Thread.MAX_PRIORITY]\n");
System.out.println(getPriorityInfo());
}
t2.join(); if (t2.getPriority() != Thread.MAX_PRIORITY) {
System.out.print("thread priority for t2 while running was " + t2.getPriority() + " [expected Thread.MAX_PRIORITY]\n");
System.out.println(getPriorityInfo());
} if (t2.nativePriority != Thread.MAX_PRIORITY) {
System.out.print("thread priority for t2 was " + t2.nativePriority + " [expected Thread.MAX_PRIORITY]\n");
System.out.println(getPriorityInfo());
} if (t2.reportedPriority != Thread.MAX_PRIORITY) {
System.out.println("Reported Java priority is wrong");
} if (t2.reportedNiceness != NICENESS_UNKNOWN && t2.reportedNiceness >= 0) { // Niceness should correspond to MAX_PRIORITY.
System.out.println("Reported Java niceness is wrong");
} Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
PriorityStoringThread t3 = new PriorityStoringThread(true);
t3.start();
t3.join(); if (t3.nativePriority != Thread.MAX_PRIORITY) {
System.out.print("thread priority for t3 was " + t3.nativePriority + " [expected Thread.MAX_PRIORITY]\n");
System.out.println(getPriorityInfo());
}
} if (Thread.currentThread().getPriority() != Thread.NORM_PRIORITY) {
System.out.println("Parent priority unexpectedly altered.");
} if (haveNicenessApis()) {
PriorityStoringThread t4 = new PriorityStoringThread(false);
VMRuntime.getRuntime().setThreadNiceness(t4, 19); // Since priority is inherited on creation, this should affect child thread.
t4.start();
t4.join(); if (supportsThreadPriorities() && t4.nativePriority != Thread.MIN_PRIORITY) {
System.out.print("Native thread priority for t4 was " + t4.nativePriority + " [expected Thread.MIN_PRIORITY]\n");
System.out.println(getPriorityInfo());
} // t4's children should have MAX_PRIORITY, since only niceness was changed. if (t4.reportedPriority != Thread.NORM_PRIORITY) {
System.out.println("Reported t4 Java priority (" + t4.reportedPriority + ") should be " + Thread.NORM_PRIORITY);
} if (t4.reportedNiceness != 19) { // Niceness should correspond to 19.
System.out.println("Reported Java niceness is wrong");
}
}
for (Thread t : threads) { if (t == current) {
System.out.println("Found current Thread in ThreadGroup"); return;
}
} thrownew RuntimeException("Did not find main thread: " + Arrays.toString(threads));
}
privatestaticvoid testMainThreadAllStackTraces() {
StackTraceElement[] trace = Thread.getAllStackTraces().get(Thread.currentThread()); if (trace == null) { thrownew RuntimeException("Did not find main thread: " + Thread.getAllStackTraces());
}
List<StackTraceElement> list = Arrays.asList(trace);
Iterator<StackTraceElement> it = list.iterator(); while (it.hasNext()) {
StackTraceElement ste = it.next(); if (ste.getClassName().equals("Main")) { if (!ste.getMethodName().equals("testMainThreadAllStackTraces")) { thrownew RuntimeException(list.toString());
}
StackTraceElement ste2 = it.next(); if (!ste2.getClassName().equals("Main")) { thrownew RuntimeException(list.toString());
} if (!ste2.getMethodName().equals("main")) { thrownew RuntimeException(list.toString());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.