Static synchronization method

Keywords: Java Attribute


The lock object of the static method is the class attribute of this class.
package com.itheima.demo08.Synchronized;
/*

Thread security issues arise in ticket sales cases
 Selling non-existent tickets and duplicate tickets

Two Solutions to Thread Safety: Using Synchronization Method
 Use steps:
    1. Extract the code that accesses the shared data and put it into a method.
    2. Adding synchronized modifiers to methods

Format: Format for defining methods
 Modifier synchronized return value type method name (parameter list){
    Code that may have thread security issues (code that accesses shared data)
}

*/
public class RunnableImpl implements Runnable{

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


//Setting Thread Tasks: Selling Tickets
@Override
public void run() {
    System.out.println("this:"+this);//this:com.itheima.demo08.Synchronized.RunnableImpl@58ceff1
    //Use a dead loop to repeat the ticket selling operation
    while(true){
        payTicketStatic();
    }
}

/*
    Static synchronization method
    Who is the lock object?
    Can't be this
    this The static method takes precedence over the object.
    The lock object of the static method is the class attribute of this class - > class file object (reflection)
 */
public static /*synchronized*/ void payTicketStatic(){
    synchronized (RunnableImpl.class){
        //Judge the existence of the ticket first
        if(ticket>0){
            //Increase the probability of security problems and let the program sleep
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //Tickets exist, ticket s are sold--
            System.out.println(Thread.currentThread().getName()+"-->Selling"+ticket+"Zhang ticket");
            ticket--;
        }
    }

}

/*
    Define a synchronization method
    Synchronization 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()
    It's also this.
 */
public /*synchronized*/ void payTicket(){
    synchronized (this){
        //Judge the existence of the ticket first
        if(ticket>0){
            //Increase the probability of security problems and let the program sleep
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //Tickets exist, ticket s are sold--
            System.out.println(Thread.currentThread().getName()+"-->Selling"+ticket+"Zhang ticket");
            ticket--;
        }
    }

}

}
package com.itheima.demo08.Synchronized;

/*

Simulated ticket sales case
 Create three threads, open at the same time, and sell shared tickets

*/
public class Demo01Ticket {

public static void main(String[] args) {
    //Creating Implementation Class Objects of Runnable Interface
    RunnableImpl run = new RunnableImpl();
    System.out.println("run:"+run);//run:com.itheima.demo08.Synchronized.RunnableImpl@58ceff1
    //Create Thread class object, pass Runnable interface implementation class object in construction method
    Thread t0 = new Thread(run);
    Thread t1 = new Thread(run);
    Thread t2 = new Thread(run);
    //Call the start method to open multithreading
    t0.start();
    t1.start();
    t2.start();
}

}

Posted by ranman on Wed, 31 Jul 2019 05:08:27 -0700