3、使用锁机制lock,unlock

package com.lfy.ThreadsSynchronize;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 1、使用锁机制
 * 语法:lock(),unlock()
*/
public class TicketSellSolution3 extends Thread{

    private static int num = 50;
    
    //创建一个锁对象
    Lock l = new ReentrantLock();
    
    public TicketSellSolution3(String string) {
        super(string);
    }
    
    @Override
    public void run() {
        for(int i = 0 ; i < 50 ;i ++){
                l.lock();
                try {
                    if(num > 0){
                      Thread.sleep(10);
                      System.out.println(Thread.currentThread().getName()+"卖出一张票,剩余"+(--num)+"张");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally{
                    l.unlock();
                }
        }
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-11-28
  • 2021-05-11
  • 2021-11-21
猜你喜欢
  • 2021-06-16
  • 2021-12-18
  • 2021-08-11
  • 2022-01-07
  • 2021-12-08
  • 2021-12-06
相关资源
相似解决方案