Reflection, class loading and garbage collection & multithreaded programming

Source: Internet
Author: User

The loading mechanism of the class

1. Class loading 2. Connection 3. Initialization

Reflection

There are two types of objects in a Java program at run time: compile-time type run-time type

Viewing class information with reflection

1. Use the Class.forName () class C=class.forname ("Fully qualified class name"); 2. Call the class property to get Class object classes for classes of objects of type c=. class

3. If an instance object can call the GetClass () method, return the object's class object class C= object name. GetClass ();

You can call the class object's method to get the object and the real information for that class after you have obtained the classes object. The public properties, methods, constructors of class classes can be obtained through the GetFields (), GetMethods (), GetConstructors () methods, through the Getdeclaredfields () ... Get all the properties, methods, constructors that the class contains

Using reflection to build and manipulate objects

When you get the properties, methods, and constructors of a class through the class object, the program can use the constructor object to invoke the appropriate constructor to create the object, using the Field object and method object to make the appropriate access modification. You can also use the newinstance () method to create a new instance of the class represented by the secondary class object.

Garbage collection

1. Timing of garbage Collection 2. The Finalize () method of the object

Summary: Java class files are loaded into memory through the ClassLoader. The ClassLoader includes the root class loader, the extension classloader, and the system ClassLoader. In addition, the Java API provides a ClassLoader abstract class that enables developers to create custom class loaders by inheriting their base classes.

Reflection: Load, detect, and use a class that is completely unknown to the compiler at run time. By using the Java reflection mechanism, we can get information about the class and dynamically create the object, set the value of the member variable, invoke the method, and dynamically create and access the array.

The Finalize () method helps us to complete some of the resources that are used to dispose of objects.

Multithreaded programming

Java multithreaded programming related core classes and interfaces mainly include the thread class, the Runnable interface, the object class, all in the Java.lang package.

The thread main method is defined in the thread class, including:

Feature Object execution method: public void Start (), public void Run (), ***setname (String name), ***getname (), setpriority (int priority) .......................................................................

Static methods: Yield (), sleep (long millisec), Holdslock (object x) ... ..... ..... ..... ..... ..... ..... ..........

Runnable class

The Runnable class interface defines a common specification for all tasks that require thread execution. Just define a run () method, let's execute the code we need.

Object class

Wait (): Infinite wait until other threads invoke the same object's notify () or Notifyall ()

Wait (long timeout): Over and above the specified time interval

Notify (): Wakes the thread that is waiting on the object

Notifyall (): Wake up all ******

Creation of Threads

1. Write a class that inherits the thread, overriding the Run () method

2. Write an interface that implements Runnable, and then associate an instance of the class with the thread object

Synchronization of Threads

Using synchronization blocks and synchronization methods

        

                  

Reflection, class loading and garbage collection & multithreaded programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.