Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should ClassLoader be thread-safe?

I'm writing my custom classloader, and am wondering if I should make it thread-safe? As you can easily see, not all native Java classloaders are thread-safe, only sun.misc.Launcher.AppClassLoader does (and yet, I've checked OpenJDK sources, and in OpenJDK it is not).

Is there a reason java classloaders are not synchronized? Should custom classloaders be thread-safe?

like image 322
Oleksiy Khilkevich Avatar asked Mar 09 '11 20:03

Oleksiy Khilkevich


1 Answers

ClassLoader.loadClass() is synchronized.

Typically a custom classloader won't override this method, but it might override findClass(). Since findClass() is called by loadClass and is therefore called from a synchronized critical section, it doesn't itself need to be synchronized.

like image 174
Mark Peters Avatar answered Oct 12 '22 04:10

Mark Peters