解决多线程安全的方式
 
synchronized     隐式锁   
1.同步代码块 
2.同步方法
 
3.lock  同步锁    显式锁         lock()方法上锁    unlock() 释放锁
 
package com.wf.zhang.juc;

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

public class TestLock {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(ticket,"1号窗口").start();
        new Thread(ticket,"2号窗口").start();
        new Thread(ticket,"3号窗口").start();
    }
}


class  Ticket implements Runnable{

    private  int tick = 100;

    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true){

            //上锁
            lock.lock();

            try {
                if (tick>0){
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                    }
                    System.out.println(Thread.currentThread().getName() +"完成售票,余票为 "+ --tick);
                }
            }finally {
                //释放锁
                lock.unlock();
            }

        }
    }
}
12-22 21:05