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
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(); } } }
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 )
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(); } } }
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
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
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2021-02-05
no need synchronized What will happen
2 One answer
fql Jump to the problem location 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 Jump to the problem location 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
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
|