/** *Loads{@codejdk.net.ExtendedSocketOptions}and{@codesun.net.ext.ExtendedSocketOptions} *andinvokes{@codesun.net.ext.ExtendedSocketOptions#getInstance()}concurrentlyinathread *oftheirownandexpectstheclassloadingofboththoseclasses *tosucceed.Additionally,afterthesetasksaredone,callsthe *sun.net.ext.ExtendedSocketOptions#getInstance()andexpectsittoreturnaregistered *ExtendedSocketOptionsinstance.
*/
@Test publicvoid testConcurrentClassLoad() throws Exception { final CountDownLatch taskTriggerLatch = new CountDownLatch(4); final List<Callable<?>> tasks = new ArrayList<>();
tasks.add(new Task("jdk.net.ExtendedSocketOptions", taskTriggerLatch));
tasks.add(new Task("sun.net.ext.ExtendedSocketOptions", taskTriggerLatch)); // add a couple of tasks which call sun.net.ext.ExtendedSocketOptions#getInstance
tasks.add(new GetInstanceTask(taskTriggerLatch));
tasks.add(new GetInstanceTask(taskTriggerLatch)); final ExecutorService executor = Executors.newFixedThreadPool(tasks.size()); try { final Future<?>[] results = new Future[tasks.size()]; // submit int i = 0; for (final Callable<?> task : tasks) {
results[i++] = executor.submit(task);
} // wait for completion for (i = 0; i < tasks.size(); i++) {
results[i].get();
}
} finally {
executor.shutdownNow();
} // check that the sun.net.ext.ExtendedSocketOptions#getInstance() does indeed return // the registered instance final Object extSocketOptions = callSunNetExtSocketOptionsGetInstance(); Assert.assertNotNull(extSocketOptions, "sun.net.ext.ExtendedSocketOptions#getInstance()" + " unexpectedly returned null"); // now verify that each call to getInstance(), either in the tasks or here, returned the exact // same instance of ExtendedSocketOptions Assert.assertEquals(2, GetInstanceTask.extendedSocketOptionsInstances.size()); for (final Object inst : GetInstanceTask.extendedSocketOptionsInstances) { Assert.assertSame(inst, extSocketOptions, "sun.net.ext.ExtendedSocketOptions#getInstance()" + " returned different instances");
}
}
@Override publicClass<?> call() {
System.out.println(Thread.currentThread().getName() + " loading " + this.className); try { // let the other tasks know we are ready to trigger our work
latch.countDown(); // wait for the other task to let us know they are ready to trigger their work too
latch.await(); returnClass.forName(this.className);
} catch (Exception e) { thrownew RuntimeException(e);
}
}
}
privatestaticclass GetInstanceTask implements Callable<Object> { // keeps track of the instances returned by calls to sun.nex.ext.ExtendedSocketOptions#getInstance() // by the GetInstanceTask(s) privatestaticfinal List<Object> extendedSocketOptionsInstances = Collections.synchronizedList(new ArrayList<>()); privatefinal CountDownLatch latch;
@Override public Object call() {
System.out.println(Thread.currentThread().getName()
+ " calling sun.net.ext.ExtendedSocketOptions#getInstance()"); try { // let the other tasks know we are ready to trigger our work
latch.countDown(); // wait for the other task to let us know they are ready to trigger their work too
latch.await(); // let's call getInstance on sun.net.ext.ExtendedSocketOptions final Object inst = callSunNetExtSocketOptionsGetInstance();
extendedSocketOptionsInstances.add(inst); return inst;
} catch (Exception e) { thrownew RuntimeException(e);
}
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 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.