publicclass MyLocal extends ThreadLocal<String> {
String val; protected String initialValue() {
other = new ThreadLocal<String>(); // This should reuse the map that the containing get() created // or visa versa (i.e. instead of a second map being created).
other.set("Other"); return"Initial";
}
}
publicvoid run() {
MyLocal l = new MyLocal(); // This should pick up the initial value
String s1 = l.get(); // And this should pick up the other local in this thread's locals map
String s2 = other.get(); if ((s2 != null) && s2.equals("Other")) { // JMM guarantees this will be visible to // another thread joining with this thread's // termination: no need for this to be volatile.
passed = true;
}
}
publicstaticvoid main(String[] args) { // Starting with Mustang the main thread already has an initialized // ThreadLocal map at this point, so test with a second thread. Thread t = newThread(new InitialValue());
t.start(); try {
t.join();
} catch (InterruptedException e) { thrownew RuntimeException("Test Interrupted: failed");
} if (!passed) { thrownew RuntimeException("Test Failed");
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-10)
¤
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.