inotgo.com


Tool version compatibility problem
Class object concept : All classes , There is a Class object , This class object is used to provide Class itself Information about , For example, there are several construction methods , How many attributes , What are the common methods .
Correct


7 branch 13 second
This video uses html5 Play mode , If it cannot be played normally , Please upgrade your browser to the latest version , Recommend Firefox ,chrome,360 browser . If thunderbolt is installed , Play the video and show the direct download status , Please adjust Thunderbolt system settings - Basic settings - Start - Monitor all browsers ( Remove this option ). chrome of Video download Plug-in will affect playback , as IDM etc. , Please close or switch other browsers

Step 1 : What is a class object   
Step 2 : Get class object   
Step 3 : When getting class objects , Will cause class properties to be initialized   
Step 4 : practice - Add... To the static method synchronized, What is the synchronization object ?   
Step 5 : answer - Add... To the static method synchronized, What is the synchronization object ?   

Before understanding class objects , Let's talk about the differences between familiar objects :
garen and teemo It's all Hero Object , The difference between them is , Each has Different names , Blood volume , Damage value .

Then talk about the differences between classes
Hero and Item They are all classes , The difference between them is that they have Different methods , Different properties .

Class object , Is used to describe this kind of , What properties do they have , What method
Getting class objects has 3 Two ways
1. Class.forName
2. Hero.class
3. new Hero().getClass()

In a JVM In , A kind of , Only one class object exists . Therefore, the class objects obtained in the above three ways , It's all the same .

notes : To be exact, it is a ClassLoader Next , A kind of , Only one class object exists . Usually one JVM Next , There will only be one ClassLoader. Because... Has not been introduced yet ClassLoader concept , So don't start for the time being .
package reflection; import charactor.Hero; public class TestReflection { public static void main(String[] args) { String className = "charactor.Hero"; try { Class pClass1=Class.forName(className); Class pClass2=Hero.class; Class pClass3=new Hero().getClass(); System.out.println(pClass1==pClass2); System.out.println(pClass1==pClass3); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package reflection;

import charactor.Hero;

public class TestReflection {

	public static void main(String[] args) {
			String className = "charactor.Hero";
			try {
				Class pClass1=Class.forName(className);
				Class pClass2=Hero.class;
				Class pClass3=new Hero().getClass();
				System.out.println(pClass1==pClass2);
				System.out.println(pClass1==pClass3);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}
Step 3 :

When getting class objects , Will cause class properties to be initialized

edit top fracture
by Hero Add a static attribute , And initialize in the static initialization block , reference resources Class property initialization .

static String copyright;
static {
System.out.println(" initialization copyright");
copyright = " Copyright by Riot Games Company owned ";
}

No matter what way to get class objects , Will cause static properties to be initialized , And it will only be executed once .( In addition to direct use Class c = Hero.class This way , This does not cause static properties to be initialized )
 When getting class objects , Will cause class properties to be initialized
package charactor; public class Hero { public String name; public float hp; public int damage; public int id; static String copyright; static { System.out.println(" initialization copyright"); copyright = " Copyright by Riot Games Company owned "; } }
package reflection; import charactor.Hero; public class TestReflection { public static void main(String[] args) { String className = "charactor.Hero"; try { Class pClass1=Class.forName(className); Class pClass2=Hero.class; Class pClass3=new Hero().getClass(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Step 4 :

practice - Add... To the static method synchronized, What is the synchronization object ?

edit top fracture   Wrong posture , get half the results with double the effort ! Click to see the correct posture for the exercise
There was an exercise before , practice - Add a modifier in front of the class synchronized

Before the object method , Add a modifier synchronized , The synchronization object is the current instance .
So if before the class method , Add a modifier synchronized, What is the synchronization object ?

Write code to verify
Step 5 :

answer - Add... To the static method synchronized, What is the synchronization object ?

edit top fracture
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Viewing this answer will cost 3 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method
Viewing this answer will cost 3 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method
Account not activated Account not activated , Limited functionality . Please click activate
When synchronized When decorating static methods , The synchronization object is the class object of this class .

As the example in the code , When the first thread enters method1 When I was , Need to occupy TestReflection.class To execute .

The second thread enters method2 I can't get in when , Only when the first thread releases the pair TestReflection.class Occupation of , To execute . Push back , The second thread also needs to occupy TestReflection.class. So TestReflection.class namely method2 Synchronization object for .

let me put it another way , Static methods are modified to synchronized When I was , The synchronization object is the class object of the current class .
package reflection; public class TestReflection { public static void main(String[] args) throws InterruptedException { Thread t1= new Thread(){ public void run(){ // Call method1 TestReflection.method1(); } }; t1.setName(" The first thread "); t1.start(); // Ensure that the first thread calls method1 Thread.sleep(1000); Thread t2= new Thread(){ public void run(){ // Call method2 TestReflection.method2(); } }; t2.setName(" The second thread "); t2.start(); } public static void method1() { synchronized (TestReflection.class) { // For method1 for , The synchronization object is TestReflection.class, Only occupy TestReflection.class Can be executed here System.out.println(Thread.currentThread().getName() + " Into method1 method "); try { System.out.println(" function 5 second "); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static synchronized void method2() { // For mehotd2 for , There must be a synchronization object , Through observation, it is found that , When a thread is method1 In , Occupied TestReflection.class Then // You can't enter method2, Infer that ,method2 Synchronization object for , namely TestReflection.class System.out.println(Thread.currentThread().getName() + " Into method2 method "); try { System.out.println(" function 5 second "); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2021-03-15 Class object
Sen Da

What is the use of class objects







The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2021-02-05 no need synchronized What will happen
An autumn dream

Is there any synchronized There's no difference in running ?




2 One answer

fql
Answer time :2021-09-21
package JavaSeHigh.reflection; public class TestReflection_synchronized { } class TestReflection3 { public static void main(String[] args) throws InterruptedException { Thread t1= new Thread(){ public void run(){ // Call method1 TestReflection3.method1(); } }; t1.setName(" The first thread "); t1.start(); // Ensure that the first thread calls method1 Thread.sleep(1000); Thread t2= new Thread(){ public void run(){ // Call method2 TestReflection3.method2(); } }; t2.setName(" The second thread "); t2.start(); Thread t3= new Thread(){ public void run(){ // Call method2 TestReflection3.method3(); } }; t3.setName(" The third thread mischievously broke into "); t3.start(); } public static void method1() { synchronized (TestReflection3.class) { // For method1 for , The synchronization object is TestReflection3.class, Only occupy TestReflection.class Can be executed here System.out.println(Thread.currentThread().getName() + " Into method1 method "); try { System.out.println(" function 15 second "); Thread.sleep(15000); System.out.println("method1 Method end "); } catch (InterruptedException e) { e.printStackTrace(); } } } public static synchronized void method2() { // For mehotd2 for , There must be a synchronization object , Through observation, it is found that , When a thread is method1 In , Occupied TestReflection.class Then // You can't enter method2, Infer that ,method2 Synchronization object for , namely TestReflection.class System.out.println(Thread.currentThread().getName() + " Into method2 method "); try { System.out.println(" function 3 second "); Thread.sleep(3000); System.out.println("method2 Method end "); } catch (InterruptedException e) { e.printStackTrace(); } } public static void method3() { // For mehotd2 for , There must be a synchronization object , Through observation, it is found that , When a thread is method1 In , Occupied TestReflection.class Then // You can't enter method2, Infer that ,method2 Synchronization object for , namely TestReflection.class System.out.println(Thread.currentThread().getName() + " Into method3 method "); try { System.out.println(" Whistling ~~~ No synchronized, I'm making trouble here 3 second "); Thread.sleep(3000); System.out.println("method3 Method end , bye-bye ~"); } catch (InterruptedException e) { e.printStackTrace(); } } }

Sea heart
Answer time :2021-03-03
You put method1 Change the sleep time to 20s Look again . Go further stay sleep Then add System.out.println("method1 end "); System.out.println("method2 end "); You can see method2 It must be method1 Start execution after execution . and method2 No synchronized Time ,method2 stay method1 Execute before completion . --------------method2 Add synchronized Time ------------------------ The first thread Into method1 method function 20 second method1 end The second thread Into method2 method function 5 second method2 end -------------------------------------------- --------------method2 No synchronized Time ------------------------ The first thread Into method1 method function 20 second The second thread Into method2 method function 5 second method2 end method1 end --------------------------------------------



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-11-20 stationmaster , This infringes your copyright
2020-10-03 Add... Before static method synchronized
2020-07-28 hero.getClass and new hero().getClass What's different ?


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 15 Previous questions , please Click to view

Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about JAVA Advanced - Reflection mechanism - Get class object Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot