Home > Back-end >  Java custom class loading new highly loaded classes will be used?
Java custom class loading new highly loaded classes will be used?

Time:11-22

Under the general logic of this code is that in the main method has two parts, the first part is to start a thread, the thread will be after 2 hours, using custom this reload MyService this class, the second part is in the main thread, have an infinite loop, will continue to call the method that MyService,

If after 1 hour, we modified the MyService. The class of the execute method,

Then in the second hour, we will custom this initiative to load the modified class files, after loaded, we in the main thread, in the new MyService, using the new MyService?

 public class App 
{
Public static void main (String [] args)
{
Thread t=new Thread (() - & gt; {
Try {
TimeUnit. HOURS. Sleep (2);
{} catch InterruptedException (e)
}
//after sleep for two hours, using UrlClassloader specified path from the hard disk loading MyService. Class files
//UrlClassloader XXX...
});
t.start();

//XXXXXXX
While (true) {
MyService service=new MyService ();
Service. The execute ();
}
}
}

CodePudding user response:

Which brother help have a look at? thank you

CodePudding user response:

Has been loaded should not load again.
If you want to achieve the effect of heat load, should not so simple to play, according to my understanding, after you success in thermal load, the new new objects are not important, before the new object will also adopt the method of new loading class area.

Modify the code can refer to the eclipse debug mode can take effect immediately, now spring environment, object is assembled when it is started

CodePudding user response:

URLClassLoader seems to also want to check the bootstrap classload before loading, unless you are to write a, don't do parents, but I estimate is can't use new to this way to create objects, get class reflection should be able to ensure that is not the bootstrap classload loaded

CodePudding user response:

Are not tested well, free play

CodePudding user response:

The same type of loader after class is loaded into the cache as if, according to the name of the class to store the findClass is to find whether the loaded by name, so you two hours after class may not be loaded

CodePudding user response:

I understand is, even if you can use your own class loader loads, this class will not use your App loaded MyService, class loader in the virtual machine + class qualified name global positioning to a class, you change the class loader is equivalent to another class.

For example we deployed in tomcat two identical application, although in some kind of global qualified name (the package name and class name) the same class, but the call won't appear across projects calls, because of the different project class files with different class loader loads

CodePudding user response:

reference 6 floor faith. Huan response:
I understand is old, even if you can use your own class loader loaded into, this class will not use your App loaded MyService, class loader in the virtual machine + class qualified name global positioning to a class, you change the class loader is equivalent to another class.

For example we deployed in tomcat two identical application, while in some kind of global qualified name (the package name and class name) the same class, but the call won't appear across projects calls, because of the different project class files using different class loaders loading


I also remember there is a rule, is using the current priority for class loader loads, in this case, AppClassLoader load the main method of our App. Class.
In the process of execution, it encountered MyService, this class code execution point now in the App. The class, the class is loaded by AppClassLoader, AppClassLoader now will be used for loading MyService. Class. Even if shown in another thread through custom loader to load the class, but also will not be used in this context,

CodePudding user response:

Has been loaded not loaded again!

This custom, will ask the father loader before loading AppClassLoader whether can load, and parent can be seen in the cache, if they loaded, if has been loaded, is no longer to load,

CodePudding user response:

Custom class loader, the custom of the loader object, waiting for the loading of the target class, waiting for the loading of the target class of objects, the first thing to distinguish the four content of the appeal, to understand the difference,

Second to understand how the JVM to distinguish different classes, in addition to the class name, and class loader object address of an object, that is, different loader object after loading the class with the same name, the JVM will identify into two different classes,

Old and new object is not the same as belonging to the class (though the same name), so the code at runtime to abnormal, you cannot use the old class reference point to the new class of objects,

How is relatively reasonable to solve the above problem:
1. Declare an interface class, do not participate in the process of custom loading, the interface function as a function of the class to be loaded, and, to be loaded classes to implement this interface,
2. Parents loading mechanism requires that all classes are delegated to the first parent loader object to load, so, need a skill will be loaded classes only by a custom loader to load,
The parents, not inheritance, but object relationships,
Stay loaded classes, how to avoid parents load, the general is to be loaded classes in another directory, equivalent for custom loader to create a unique classpath, so programming is more simple,
3. How to use the new load in the object of the class? Can be operated through the interface,
4. The multi-thread asynchronous loading need to be aware of a few items:
A. due to the timing of the asynchronous loading can't accurate grasp, then load the class files, class files may be generated or are sent to the specified directory, then the loading process will throw an exception, complete file changes and file changes, are two concepts,
B. stay loaded class object, to design life cycle management, new generation of object to be initialized, as objects to be uninstalled, prevent memory leaks and logic is incomplete,
C. each kind of load operation, if a change of the file, is must use the new object class loader to load, each loader object will be set in the cache, the loaded classes generally will not be secondary load,
D. the new load in class, are instantiated by reflecting the operation, will be good, then, with the reference of the interface point to the new instantiated object, to the subsequent function calls,
E. if is multithreaded programming, attention to the problem of thread safe,



CodePudding user response:

refer to the original poster Sun Dacheng _SunRobin response:
under the general logic of this code is that in the main method has two parts, the first part is to start a thread, the thread will be after 2 hours, using custom this reload MyService this class, the second part is in the main thread, have an infinite loop, will continue to call the method that MyService,

If after 1 hour, we modified the MyService. The class of the execute method,

Then in the second hour, we will custom this initiative to load the modified class files, after loaded, we in the main thread, in the new MyService, using the new MyService?

 public class App 
{
Public static void main (String [] args)
{
Thread t=new Thread (() - & gt; {
Try {
TimeUnit. HOURS. Sleep (2);
{} catch InterruptedException (e)
}
//after sleep for two hours, using UrlClassloader specified path from the hard disk loading MyService. Class files
//UrlClassloader XXX...
});
t.start();

//XXXXXXX
While (true) {
MyService service=new MyService ();
Service. The execute ();
}
}
}


Lifetimes to tell you, no,

All used, the main thread by app loader loaded object, including the inside of the infinite loop behind MyService class, and, due to the cache object of loader, so will not be loaded again,

Child thread class loading after 2 hours of sleep, will not affect the main thread, two pieces of code are isolated, or relatively independent, and no obvious contact,
  • Related