---
title: "并发控制实现工具类"
author: "lishoubin_123"
date: "2015-03-05T14:36:11Z"
tags: ["并发"]
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2015-03-05T14:36:11Z"
id: 2189077
url: "https://lishoubin.iteye.com/blog/2189077"
---

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

public class YshdTools {

    public static final String  yszfsuccess="5";//预售活动支持成功短信  
    public static final String  ystijdd="4";//预售活动提交订单  
    /\**  
     * 单利模式的锁  
     \*/  
    private static ReentrantLock LOCK;  
    /\**  
     * 队列最大排队数量  
     \*/  
    public static final Integer QUEUE\_MAX = 50;

    /\**  
     * 返回当前系统的锁  
     * @return  
     \*/  
    private static ReentrantLock getInstance() {  
        if (LOCK == null) {  
            LOCK = new ReentrantLock();  
        }  
        return LOCK;  
    }  
    /\**  
     * 锁定  
     * 如果锁被其他线程调用，则处于阻塞状态  
     * 当锁被释放时，才继续执行  
     \*/  
    public static void lock() {  
        getInstance().lock();  
    }  
    /\**  
     * 解锁  
     * 当需要锁定的程序执行完之后，此方法必须调用  
     * 因为此方法的实现为代码级别，所以在虚拟机抛出异常时，不会自动解锁  
     * 最好放在 finally 里面  
     \*/  
    public static void unlock() {  
        getInstance().unlock();  
    }  
    /\**  
     * 过去队列个数  
     * 即阻塞中的线程个数  
     * @return  
     \*/  
    public static int getQueueLength() {  
        return getInstance().getQueueLength();  
    }  
    /\**  
     * 查看锁是否为锁定状态  
     * @return  
     \*/  
    public static boolean isLock() {  
        return getInstance().isLocked();  
    }

    /\**  
     *  
    * 创建人：Administrator    
    * 创建时间：Jul 11, 2014   
    * @return String    返回类型  
    * @Description: 生成6为随机数数字  
     \*/  
    public static String getRandom(){  
        Random random = new Random();  
        String result="";  
        for(int i=0;i&lt;6;i++){  
            result+=random.nextInt(10);  
        }  
        return result;  
    }  
}