java understands static synchronization method, thinking about. class and getClass

//Define a ticket source shared by multiple threads
private static int ticket = 100;


//Set thread task: ticket selling
@Override
public void run() {
    System.out.println("this:"+this);//this:com.itheima.demo08.Synchronized.RunnableImpl@58ceff1
    //Use the dead cycle to repeat the ticket selling operation
    while(true){
        payTicketStatic();
    }
}

/*
    Static synchronization method
    Who is the object of the lock?
    It can't be this
    this Generated after the creation of objects, static methods take precedence over objects
    The lock object of static method is the class attribute of this class -- > class file object (reflection)
 */
public static /*synchronized*/ void payTicketStatic(){
    synchronized (RunnableImpl.class){
        //Judge whether the ticket exists first
        if(ticket>0){
            //Improve the probability of safety problems and let the program sleep
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //ticket exists--
            System.out.println(Thread.currentThread().getName()+"-->Selling first"+ticket+"Zhang ticket");
            ticket--;
        }
    }

}

/*
    Define a synchronization method
    The synchronization method also locks the code inside the method
    Let only one thread execute
    Who is the lock object of the synchronization method?
    Is to implement the class object new RunnableImpl()
    That's what
 */
public /*synchronized*/ void payTicket(){
    synchronized (this){
        //Judge whether the ticket exists first
        if(ticket>0){
            //Improve the probability of safety problems and let the program sleep
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //ticket exists--
            System.out.println(Thread.currentThread().getName()+"-->Selling first"+ticket+"Zhang ticket");
            ticket--;
        }
    }

}

The object lock must be the same. Here, RunnableImpl.class solves the problem that the object called by synchronized is not this, because static code does not need to create an object instance. What does the. Class class represent? :

Here we refer to: https://blog.csdn.net/xiaokang123456kao/article/details/72859765

The differences between the two are as follows:
Class name. Class is called "class literal". Because class is a keyword, class name. Class is determined at compile time. GetClass () is called by a specific method. It is determined by the actual instance at runtime. GetClass () is dynamic and final.
For example:
String.class can obtain the reference of class object of this type in memory by reference to class name, while new String().getClass() can obtain the reference of class object of this actual type in memory by instance object.
We can use a small example to see the difference between the two:

package com.kang;
public abstract class Animal {
}
`

```java
 Insert a code slice here
package com.kang;

public class Dog extends Animal {
    public static void main(String[] args) {
        Animal animal = new Dog();
        System.out.println(animal.getClass().getName());
        //Output com.kang.Dog
        System.out.println(Animal.class.getName());
        //Output com.kang.Animal
    }
}

com.kang.Dog
com.kang.Animal
Explain.
animal.getClass().getName() is the type of class that gets the runtime instance when the program is running. However, Animal.class.getName() is determined in the compilation phase, regardless of the state of the runtime.

At present, there is no better way to understand knowledge reserve. For the time being, it is understood that RunnableImpl.class has been set and will not be changed at the compilation stage. This object, which does not belong to a class anyway, is temporarily called a reference to the class name.

Published 28 original articles, won praise 3, visited 4520
Private letter follow

Tags: Attribute Java

Posted on Wed, 15 Jan 2020 05:45:29 -0500 by cyanblue