Overview
In this course, we mainly learned:
- JDK、JRE 与 JVM
- Java Multi-Thread
- Java‘s Reflection Mechanism
一、 JDK、JRE 与 JVM
1. JDK
Java Development Kit is the core of Java. Which includes JRE, compiler javac and basic class library.
JDK is used for the development of Java programs. The latest version is JDK 16.
2. JRE
Java Runtime Environment provides the runtime environment required by Java programs, including JVM and standard class libraries.
3. JVM
Java Virtual Machine is a fictitious computer. When JVM executes bytecode, it interprets bytecode as machine instructions on a specific platform. Therefore, Java programs can "compile once and run everywhere."
The development and execution process of a Java program is:
- Call the API in the JDK to write the source code, the suffix is .java
- The compiler javac compiles it into bytecode with the suffix .class
- JRE load, verify, and execute bytecode
- The JVM interprets the bytecode as machine instructions and executes them.
二、Java 多线程
There are two main ways to create threads in Java:
- Implement the Runnable interface
- Extend the Thread class
1. Implement the Runnable interface
如果采用这种方法,我们只需要重写其中的 run() 方法就可以实现我们想要的功能。
If this method is adopted, we only need to rewrite the run() method to achieve the function we want.
Like:
class MyThread implements Runnable{
@Override
public void run(){
for(int i = 0; i < 10; i++){
System.out.println("This is My Thread!");
}
}
}
After declaring an instance of MyThread, call the start() method to start the thread:
public static void main(String[] args){
Thread t = new Thread(new MyThread());
t.start();
}
2. Extend Thread
Another method is to extend the Thread class. We also need to rewrite the run() method.
Like:
class MyThread extends Thread{
@Override
public void run(){
for(int i = 0; i < 10; i++){
System.out.println("This is My Thread!");
}
}
}
After declaring an instance of MyThread, call the start() method to start the thread:
public static void main(String[] args){
Thread t = new MyThread();
t.start();
}
3. Common methods of the Thread class
方法 | 功能 |
---|---|
start() | 启动线程,调用 run() 方法 |
yield() | 暂停当前线程,执行其他线程 |
sleep(long millisec) | 让当前线程暂停 millisec 时间 |
currentThread() | 返回当前线程对象的引用 |
三、Java Reflection Mechanism
Reflection is an important feature of Java. It can access the properties and methods of an object during operation.
One of the most important uses of reflection is to develop a common framework. For example, Spring.
To use reflection, we need to obtain the Class object of the class. This Class object is generated by the JVM, and there is only one Class object for a class.
Java‘s java.lang.Reflect package provides the reflection function.
1. Get the Class object
There are three ways to obtain a Class object:
- 调用 Class.forname() 方法。在 JDBC 开发中,我们常用此方法加载数据库驱动。
Class clazz = Class.forName("MyClass");
- 使用类名+ .class
Class clazz = boolean.class;
- 调用 Object 类的 getClass() 方法
Class clazz = myclass.getClass();
2. Get the required information
We can use the Field, Method, and Constructor classes in java.lang.Reflect to get the members, methods, and constructors of the object.