SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
Class Loader
By: Prateek Jain
Fundamentals
1. Class loader delegation
a. The class loader delegation model is the graph of class loaders that pass
loading requests to each other.
b. The bootstrap class loader is the root of this graph.
c. Class loaders are created with a single delegation parent and looks for class
at:

i. Cache.
ii. Parent.
iii. Self.
Fundamentals
2. The parent class loader is always given the opportunity to load a class first.
3. Due to point #2, a classloader can only see classes loaded by itself or its
parent/ancestor classloaders and not by children.
4. The bootstrap class loader cannot be instantiated by java code.
Fundamentals
5. The extension (standard extensions) class loader is responsible to load
classes from the extensions directory (jre/lib/ext).
6. The system (application)class loader is responsible for loading code from
the path specified by the CLASSPATH environment variable. This can be
returned by:
ClassLoader.getySystemClassLoader();
Phases of class loading
1. Loading
2. Linking
3. Initializing
Phases explained
1. Loading phase
a. Consists of locating the required class file and loading in the
bytecode.
b. It gives a very basic memory structure to the class object.
c. Methods, fields and other referenced classes are not dealt with at this
stage.
Phases explained
1. Linking phase
a. Bytecode verification, the class loader performs checks on the bytecodes of
the class to ensure it is well formed and well behaved.
b. Class preparation, prepares the necessary data structures within each class
like fields, methods and implemented interfaces.
c. Resolving, the class loader loads all the other classes referenced by
particular class.
Phases explained
1. Initializing phase
a. Any static initializers contained within a class are executed.
Note: At the end of this phase, all static fields are given their default values.
Loading Types
Explicit loading
1. via, cl.loadClass() [cl is an instance of ClassLoader].
2. Class.forName().

When one of these methods is invoked, the class whose name is specified as an argument is loaded
by the class loader. If the class is already loaded, then a reference is simply returned; otherwise,
the loader goes through the delegation model to load the class.
Loading Types
Implicit loading
Occurs when a class is loaded as result of a reference, instantiation, or
inheritance (not via an explicit method call). In each of these cases, the
loading is initiated under the covers and the JVM resolves the necessary
references and loads the class.
Problems with class
loaders
1. Example1
2. Visibility of classes.
Problems with class
loaders
3. When over riding loadClass().
If class loaders only use the standard delegation model, then there is no need to override the
loadClass() method. However, if a different model is required, then loadClass() must be
overridden, in which case there are special considerations that must be taken into account.
public Class loadClass(String name) throws ClassNotFoundException
{
return findClass(name);
}

Although this looks reasonable, a call to this method results in the
following exception:
Exception in thread "main" java.lang.NoClassDefFoundError:
java/lang/Object
Because the overridden loadClass() method never delegates to its
parent.
Problems with class
loaders
improved implementation
public Class loadClass(String name) throws ClassNotFoundException {
Class c = null;
try {
c = getParent().loadClass(name);
} catch (ClassNotFoundException e) {
}
if(c == null)
c = findClass(name);
return c;
}
Problems with class
loaders
Problem with serialization and GC
The collector examines the class loader data structures to determine which
classes are live -- that is, are not garbage collectable.
When the class loader is dereferenced, the classes that it loaded are not garbage collectable. This is
because there is a live reference to the serialized class from the ObjectStreamClass lookup table.
ObjectStreamClass is a primordial class and therefore is never garbage collected. The lookup
table is referenced from a static field in ObjectStreamClass and is kept in the class itself rather
than in an instance of it. As a result, the reference to serialized class exists for the lifetime of the
JVM, and the class thus cannot be garbage collected. Importantly, the serialized class has a
reference to its defining class loader, and so it cannot be completely dereferenced either.
Problems with class
loaders
Class loader deadlocks
A class loader deadlock occurs when two threads each own a lock on two different class loaders, and
both threads are waiting for the lock owned by the other thread. Both threads will wait indefinitely
for the lock on the other class loader, and so they become deadlocked.
Problems with class
loaders
Deadlock Scenario

Class Hierarchy:
class A extends B
class C extends D
ClassLoader Delegation Hierarchy:
Custom Classloader CL1:
directly loads class A
delegates to custom ClassLoader CL2 for class B
Custom Classloader CL2:
directly loads class C
delegates to custom ClassLoader CL1 for class D
Thread 1:
Use CL1 to load class A (locks CL1)
defineClass A triggers
loadClass B (try to lock CL2)
Thread 2:
Use CL2 to load class C (locks CL2)
defineClass C triggers
loadClass D (try to lock CL1)
Solution to deadlock
The Java SE 7 release includes the concept of a parallel capable class loader.
Loading a class by a parallel capable class loader now synchronizes on the
pair consisting of the class loader and the class name.
Thread 1:
Use CL1 to load class A (locks CL1+A)
defineClass A triggers
loadClass B (locks CL2+B)
Thread 2:
Use CL2 to load class C (locks CL2+C)
defineClass C triggers
loadClass D (locks CL1+D)

for more details: http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html
Q&A

Weitere ähnliche Inhalte

Was ist angesagt?

Understanding Java Dynamic Proxies
Understanding Java Dynamic ProxiesUnderstanding Java Dynamic Proxies
Understanding Java Dynamic ProxiesRafael Luque Leiva
 
55 new things in Java 7 - Devoxx France
55 new things in Java 7 - Devoxx France55 new things in Java 7 - Devoxx France
55 new things in Java 7 - Devoxx FranceDavid Delabassee
 
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIsCS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIsKwangshin Oh
 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Abhishek Khune
 
Basics of java programming language
Basics of java programming languageBasics of java programming language
Basics of java programming languagemasud33bd
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Kernel Training
 
Class method object
Class method objectClass method object
Class method objectMinal Maniar
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In JavaMD SALEEM QAISAR
 
Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz SAurabh PRajapati
 

Was ist angesagt? (20)

Understanding Java Dynamic Proxies
Understanding Java Dynamic ProxiesUnderstanding Java Dynamic Proxies
Understanding Java Dynamic Proxies
 
Class loaders
Class loadersClass loaders
Class loaders
 
Dynamic Proxy by Java
Dynamic Proxy by JavaDynamic Proxy by Java
Dynamic Proxy by Java
 
55 new things in Java 7 - Devoxx France
55 new things in Java 7 - Devoxx France55 new things in Java 7 - Devoxx France
55 new things in Java 7 - Devoxx France
 
Java basics
Java basicsJava basics
Java basics
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Java Tutorial 1
Java Tutorial 1Java Tutorial 1
Java Tutorial 1
 
Basics of java
Basics of javaBasics of java
Basics of java
 
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIsCS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)
 
Basics of java programming language
Basics of java programming languageBasics of java programming language
Basics of java programming language
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
2. hello java
2. hello java2. hello java
2. hello java
 
Java Intro
Java IntroJava Intro
Java Intro
 
Class method object
Class method objectClass method object
Class method object
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In Java
 
Java basic introduction
Java basic introductionJava basic introduction
Java basic introduction
 
Java bcs 21_vision academy_final
Java bcs 21_vision academy_finalJava bcs 21_vision academy_final
Java bcs 21_vision academy_final
 
Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz
 

Ähnlich wie Java Classloaders (20)

Diving into Java Class Loader
Diving into Java Class LoaderDiving into Java Class Loader
Diving into Java Class Loader
 
Class
ClassClass
Class
 
Class
ClassClass
Class
 
Class
ClassClass
Class
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
 
Static binding
Static bindingStatic binding
Static binding
 
Java14
Java14Java14
Java14
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
Perils Of Url Class Loader
Perils Of Url Class LoaderPerils Of Url Class Loader
Perils Of Url Class Loader
 
Java assignment help
Java assignment helpJava assignment help
Java assignment help
 
Java tips
Java tipsJava tips
Java tips
 
LISP: Object Sytstem Lisp
LISP: Object Sytstem LispLISP: Object Sytstem Lisp
LISP: Object Sytstem Lisp
 
LISP:Object System Lisp
LISP:Object System LispLISP:Object System Lisp
LISP:Object System Lisp
 
JVM
JVMJVM
JVM
 
Java Reflection Concept and Working
Java Reflection Concept and WorkingJava Reflection Concept and Working
Java Reflection Concept and Working
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 

Kürzlich hochgeladen

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 

Kürzlich hochgeladen (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Java Classloaders

  • 2. Fundamentals 1. Class loader delegation a. The class loader delegation model is the graph of class loaders that pass loading requests to each other. b. The bootstrap class loader is the root of this graph. c. Class loaders are created with a single delegation parent and looks for class at: i. Cache. ii. Parent. iii. Self.
  • 3. Fundamentals 2. The parent class loader is always given the opportunity to load a class first. 3. Due to point #2, a classloader can only see classes loaded by itself or its parent/ancestor classloaders and not by children. 4. The bootstrap class loader cannot be instantiated by java code.
  • 4.
  • 5. Fundamentals 5. The extension (standard extensions) class loader is responsible to load classes from the extensions directory (jre/lib/ext). 6. The system (application)class loader is responsible for loading code from the path specified by the CLASSPATH environment variable. This can be returned by: ClassLoader.getySystemClassLoader();
  • 6. Phases of class loading 1. Loading 2. Linking 3. Initializing
  • 7.
  • 8. Phases explained 1. Loading phase a. Consists of locating the required class file and loading in the bytecode. b. It gives a very basic memory structure to the class object. c. Methods, fields and other referenced classes are not dealt with at this stage.
  • 9. Phases explained 1. Linking phase a. Bytecode verification, the class loader performs checks on the bytecodes of the class to ensure it is well formed and well behaved. b. Class preparation, prepares the necessary data structures within each class like fields, methods and implemented interfaces. c. Resolving, the class loader loads all the other classes referenced by particular class.
  • 10. Phases explained 1. Initializing phase a. Any static initializers contained within a class are executed. Note: At the end of this phase, all static fields are given their default values.
  • 11. Loading Types Explicit loading 1. via, cl.loadClass() [cl is an instance of ClassLoader]. 2. Class.forName(). When one of these methods is invoked, the class whose name is specified as an argument is loaded by the class loader. If the class is already loaded, then a reference is simply returned; otherwise, the loader goes through the delegation model to load the class.
  • 12. Loading Types Implicit loading Occurs when a class is loaded as result of a reference, instantiation, or inheritance (not via an explicit method call). In each of these cases, the loading is initiated under the covers and the JVM resolves the necessary references and loads the class.
  • 13. Problems with class loaders 1. Example1 2. Visibility of classes.
  • 14. Problems with class loaders 3. When over riding loadClass(). If class loaders only use the standard delegation model, then there is no need to override the loadClass() method. However, if a different model is required, then loadClass() must be overridden, in which case there are special considerations that must be taken into account. public Class loadClass(String name) throws ClassNotFoundException { return findClass(name); } Although this looks reasonable, a call to this method results in the following exception: Exception in thread "main" java.lang.NoClassDefFoundError: java/lang/Object Because the overridden loadClass() method never delegates to its parent.
  • 15. Problems with class loaders improved implementation public Class loadClass(String name) throws ClassNotFoundException { Class c = null; try { c = getParent().loadClass(name); } catch (ClassNotFoundException e) { } if(c == null) c = findClass(name); return c; }
  • 16. Problems with class loaders Problem with serialization and GC The collector examines the class loader data structures to determine which classes are live -- that is, are not garbage collectable. When the class loader is dereferenced, the classes that it loaded are not garbage collectable. This is because there is a live reference to the serialized class from the ObjectStreamClass lookup table. ObjectStreamClass is a primordial class and therefore is never garbage collected. The lookup table is referenced from a static field in ObjectStreamClass and is kept in the class itself rather than in an instance of it. As a result, the reference to serialized class exists for the lifetime of the JVM, and the class thus cannot be garbage collected. Importantly, the serialized class has a reference to its defining class loader, and so it cannot be completely dereferenced either.
  • 17. Problems with class loaders Class loader deadlocks A class loader deadlock occurs when two threads each own a lock on two different class loaders, and both threads are waiting for the lock owned by the other thread. Both threads will wait indefinitely for the lock on the other class loader, and so they become deadlocked.
  • 18. Problems with class loaders Deadlock Scenario Class Hierarchy: class A extends B class C extends D ClassLoader Delegation Hierarchy: Custom Classloader CL1: directly loads class A delegates to custom ClassLoader CL2 for class B Custom Classloader CL2: directly loads class C delegates to custom ClassLoader CL1 for class D Thread 1: Use CL1 to load class A (locks CL1) defineClass A triggers loadClass B (try to lock CL2) Thread 2: Use CL2 to load class C (locks CL2) defineClass C triggers loadClass D (try to lock CL1)
  • 19. Solution to deadlock The Java SE 7 release includes the concept of a parallel capable class loader. Loading a class by a parallel capable class loader now synchronizes on the pair consisting of the class loader and the class name. Thread 1: Use CL1 to load class A (locks CL1+A) defineClass A triggers loadClass B (locks CL2+B) Thread 2: Use CL2 to load class C (locks CL2+C) defineClass C triggers loadClass D (locks CL1+D) for more details: http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html
  • 20. Q&A