Design the mutex of java

來源:互聯網
上載者:User
    Java has provided synchronized key word to be used at the aspect of critical section. However it is not convenient for advanced users who want to be agile to utilize mutex. such as trying to lock it and no waiting  operation. So the mutex should be designed to be satified with these special cases.
    I have implemented these functions basing one the following the interface of mutex. It is only my ideas to make a description of java mutex. I hope that java funs can discuss it deeply, and improve and make better this interface. Thanks!

/**
 * The interface of multiplying excluding variable is used in the environment of
 * multiplying threads. It provides recurisive locking operation and the mechanism
 * of priority reversed is forbidden. Of course, it can be implemented by single CPU
 * or multiplying CPUs's API.
 *
 * Author           : Wang yanqing
 * Email            : hello.wyq@gmail.com
 * Version          : 0.001
 * Creating date    : 03/14/2006
 * Modifying date    : 03/14/2006
 * Copyright        : free usage and no modification
 * History          :
 * [03/14/2006]
 *    Define IMutex interface
 */

package osa.mutex;

import osa.common.Waiter;

public interface IMutex
{
    /**
     * In normal case, recurisive mutex will be created, and same owner of mutex
     * can be entered critical setion repeatly. If this property is set, mutex should
     * disable recurisive property, and only permit once.
     */
    public final static int IGNORE_RECURISION     = 0x01;

    /**
     * When the thread of lower priority occupies mutex, the high ones will be blocked
     * and enter sleeping queue of object. After exiting critical setion the thread of lower
     * priority release mutex, however, threads of ile priority  will maybe acquire mutex
     * even if threads of high priority are requesting mutex at the same time. It can not
     * be permitted in real-time or others special environments. If the property is not set,
     * mutex will consider priority of thread, and compete mutex according to priority of
     * of threads.
     */
    public final static int IGNORE_PRIORITY        = 0x02;

    /**
     * It is used by locking function to indicate whether threads will wait some milliseconds
     * to obtain mutex. 'NO_WAIT' means that threads will immediately return error codes
     * when mutex has been occupied by others
     */
    public final static long NO_WAIT            = Waiter.NO_WAIT;

    /**
     * It is used by locking function to indicate whether threads will wait some milliseconds
     * to obtain mutex. 'WAIT_FOREVER' means that threads will wait for mutex until
     * mutex has been free by others.
     */
    public final static long WAIT_FOREVER        = Waiter.WAIT_FOREVER;
   
    /**
      * Acquire mutex within the range of several milliseconds.
      * If 'IGNORE_RECURISION' has been set, error information should be printed to
      * remind that user has bugs to utilize mutex and it makes dead-lock.
      *
      * @param timeout  --- milliseconds of timeout to wait to obtain mutex, the value must >= 0
      *                     NO_WAIT, WAIT_FOREVER or idiographic value can be used.
      * @return Result.EINV     --- error parameter(When timeout < 0)
      *         Result.ETIMEOUT --- timeout(When timeout equals 'WAIT_FOREVER', it
      *                             should never be returned)
      *         Result.ESYS     ---system has errors, such as failing to acquire current thread
      *         Result.OK       --- success
      * @since 0.001
      */
    public int lock( long timeout );

    /**
     * Release mutex.
     * If  'IGNORE_PRIORITY' has not been set, mutex must disable to occur priority-reversion
     * phenomenon when threads of lower priority realse mutex.
     * Warnning information should be shown when locking and unlocking threads are
     * different threads. I believe that it should be same in normal case, so warnning information
     * maybe is helpful for debugger:-).
     *
     * @param  null
     * @return Result.ESTAT --- error state(when locking and unlocking operations are unmatched)
     *         Result.ESYS  --- system has errors
     *         Result.OK    --- success
     * @since 0.001
     */
    public int unlock();
}

   OSA is my main package to include all relative classes of OS, and it is an abbrevication of "Operation System Abstraction". There are mutex, queue, condition variable and so on. Different part occupies different directories. I think that it is available to manage the update of OSA later.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.