⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 semaphore.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .//package org.jahia.utils;/** * A semaphore is generally used as a synchronization object between multiple * threads or to protect a limited and finite resource such as a memory or * thread pool. The semaphore has a counter which only permits access by one * or more threads when the value of the semaphore is non-zero. Each access * reduces the current counter value of the semaphore by 1. One or more threads * can wait on a semaphore until it is no longer 0, and hence the semaphore can * be used as a simple thread synchronization object to enable one thread to * pause others until the thread is ready or has provided data for them. * * @author  Fulco Houkes * @version 1.0 */public class Semaphore{    private int mCounter;    private int mInitialCounter;    //-------------------------------------------------------------------------    /**     * Default constructor. Instanciate a semaphore with an internal counter set     * to 1. This kind of instanciation is equivalent to a lock.     */    public Semaphore ()    {        mCounter = 1;        mInitialCounter = mCounter;    }    //-------------------------------------------------------------------------    /**     * Constructor instanciating a semaphore with a user defined counter. If it's     * set to 1, it's equivalent to a lock, any higher couter value will allow     * more than once access to the critical resource.     *     * @param   counter Counter initial value.     */    public Semaphore (int counter)    {        mCounter = counter;        mInitialCounter = mCounter;    }    //-------------------------------------------------------------------------    /**     * Get a lock on the critical resource. The method returns only when     * an access could be received. When the requested lock could not be received,     * the calling object is inserted into the waiting list and will be notified     * when the lock is available again.     *     * @return  Return true when the lock could be locked, or false if any error     *          occured while trying to lock the semaphore.     */    public synchronized boolean lock ()    {        while (mCounter == 0) {            try {                wait();            }            catch (InterruptedException ex) {                return false;            }        }        mCounter--;        return true;    }    //-------------------------------------------------------------------------    /**     * Try the get a lock on the critical resource. This method doesn't wait     * until the lock is available.     *     * @return  Return true if the semaphore could be locked, or false if the     *          semaphore is unavailable.     */    public synchronized boolean tryLock ()    {        if (mCounter == 0) {            return false;        }        mCounter--;        return true;    }    //-------------------------------------------------------------------------    /**     * Release a previous locked access, notify all the waiting objects there     * is a new access available.     */    public synchronized void unlock ()    {        mCounter++;        if (mCounter > mInitialCounter) {            mCounter = mInitialCounter;        }        notify();    }    //-------------------------------------------------------------------------    /**     * Check if the semaphore is locked or not. This doesn't mean that the lock     * will still be locked or unlocked when a lock is requested after this method     * returns.     *     * @return  Return true if the semaphore is locked, or false if it's not.     */    public synchronized boolean isLocked ()    {        return (mCounter == 0);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -