java锁经典示例——卖车票场景

场景:20张车票 3个窗口同时售票

1.不加锁

package com.yao.lock;

/**
 * 不加锁的情况
 */
public class Runnable_demo implements Runnable{  
    private int ticket=20;  
    public Runnable_demo(){       
    }  
    public void run() {  
        for(int i=0;i<20;i++){  
                if(this.ticket>0){  
                    //休眠1s秒中,为了使效果更明显,否则可能出不了效果  
                    try {  
                        Thread.sleep(1000);  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                    System.out.println(Thread.currentThread().getName()+"号窗口卖出:"+this.ticket--+"号票");  
                }              
        }  
    }  
      
     public static void main(String args[]){  
         Runnable_demo demo=new Runnable_demo();  
         //基于火车票创建三个窗口  
         new Thread(demo,"a").start();  
         new Thread(demo,"b").start();  
         new Thread(demo,"c").start();  
     }  
      
} 

运行结果:

2.synchronized

package com.yao.lock;

public class Runnable_demo1 implements Runnable{  
    private int ticket=20;
    private boolean isSoldOut = false;
    public Runnable_demo1(){       
    }  
    public void run() {  
        for(int i=0;i<20;i++){  
            synchronized(this){  
                if(this.ticket>0){  
                    //休眠1s秒中,为了使效果更明显,否则可能出不了效果  
                    try {  
                        Thread.sleep(1000);  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                    System.out.println(Thread.currentThread().getName()+"号窗口卖出:"+this.ticket--+"号票");  
                }else{
                    if(!isSoldOut){
                        System.out.println("售罄");
                        isSoldOut = true;
                    }
                }
            }  
              
        }  
    }  
      
     public static void main(String args[]){  
         Runnable_demo1 demo=new Runnable_demo1();  
         //基于火车票创建三个窗口  
         new Thread(demo,"a").start();  
         new Thread(demo,"b").start();  
         new Thread(demo,"c").start();  
     }  
}

运行结果:

3.Lock

package com.yao.lock;

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

public class Runnable_demo2 {
      public static void main(String[] args) {
        // 创建3个线程对象
        SellTicket st = new SellTicket();
      
        Thread t1 = new Thread(st, "窗口1");
        Thread t2 = new Thread(st, "窗口2");
        Thread t3 = new Thread(st, "窗口3");
      
        // 启动线程
        t1.start();
        t2.start();
        t3.start();
      }
    }
      
    class SellTicket implements Runnable {
      private int ticket = 20;
      private Lock lock = new ReentrantLock();
      
      public void run() {
        while (true) {
          lock.lock();
      
          if (ticket > 0) {
            try {
              Thread.sleep(1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
      
            System.out.println(Thread.currentThread().getName() + "正在出售第" + (ticket--) + "张票。");
          }      
          lock.unlock();
        }
      }
    }

运行结果:

原文地址:https://www.cnblogs.com/vayci/p/7112081.html