JAVA study notes (multithreading 4) - thread safety 3 (thread synchronization mechanism - Lock)

JAVA study notes (multithreading 4) - thread safety 3 (thread synchronization mechanism - Lock)

Method 3: Lock (lock) (added after JDK5.0)

The method to solve the problem of thread safety (synchronization mechanism) - the third way to achieve synchronization - > Lock (lock)

  1. Starting from JDK 5.0, Java provides a more powerful thread synchronization mechanism - synchronization is achieved by explicitly defining a synchronization lock object. Synchronization locks are implemented using Lock objects.
  2. The java.util.concurrent.locks.Lock interface is a tool for controlling access to shared resources by multiple threads.
    The lock provides exclusive access to shared resources. Only one thread can lock the Lock object at a time, and the thread should obtain the Lock object before it starts to access the shared resource.
  3. The ReentrantLock class implements Lock, which has the same concurrency and memory semantics as synchronized. In implementing thread-safe control, ReentrantLock is more commonly used, which can explicitly lock and release locks.

More common interview questions: the similarities and differences between synchronized and Lock?

Same :
both can solve the thread safety problem

different :

  1. The synchronized mechanism automatically releases the synchronization monitor after executing the corresponding synchronization code
  2. Lock needs to start synchronization manually (lock() method), and end synchronization also needs to be implemented manually (unLock() method)

Suggested order of priority for synchronous methods:
Lock → synchronous code block (already entered the method body and allocated corresponding resources) → synchronous method (outside the method body)

LockTest (using the implementation of the Runnable interface to implement the thread synchronization mechanism Lock (lock))

class Windows6 implements Runnable{
    
    
    private int ticket=100;
    //1.实例化ReentrantLock
    private ReentrantLock lock=new ReentrantLock();
    @Override
    public void run() {
    
    
        try{
    
    
            //2.调用上锁方法lock()
            lock.lock();
            while (true){
    
    
                if (ticket>0){
    
    
                    try {
    
    
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖票,票号为:" + ticket);
                    ticket--;
                }else {
    
    
                    break;
                }
            }
        }finally {
    
    
            //3.调用解锁方法unlock()
            lock.unlock();
        }
    }
}
public class LockTest {
    
    
    public static void main(String[] args) {
    
    
        Windows6 windows = new Windows6();
        Thread t1 = new Thread(windows);
        Thread t2 = new Thread(windows);
        Thread t3 = new Thread(windows);
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}

LockTest1 (Using the method of inheriting the Thread class to implement the thread synchronization mechanism Lock (lock))

class Windows7 extends Thread{
    
    
    private static int ticket=100;
    //1.实例化ReentrantLock
    private static ReentrantLock lock=new ReentrantLock();
    @Override
    public void run() {
    
    
        try{
    
    
            //2.调用上锁方法lock()
            lock.lock();
            while (true){
    
    
                if (ticket>0){
    
    
                    try {
    
    
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖票,票号为:" + ticket);
                    ticket--;
                }else {
    
    
                    break;
                }
            }
        }finally {
    
    
            //3.调用解锁方法unlock()
            lock.unlock();
        }
    }
}
public class LockTest1 {
    
    
    public static void main(String[] args) {
    
    
        Windows7 windows1=new Windows7();
        Windows7 windows2=new Windows7();
        Windows7 windows3=new Windows7();

        windows1.setName("窗口1");
        windows2.setName("窗口2");
        windows3.setName("窗口3");

        windows1.start();
        windows2.start();
        windows3.start();
    }
}

Thread deadlock problem

Guess you like

Origin blog.csdn.net/m0_46450708/article/details/119036779