<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Duran's technical life
    踏踏實(shí)實(shí)學(xué)技術(shù),認(rèn)認(rèn)真真做研究。
    參考package EDU.oswego.cs.dl.util.concurrent

    1public interface Sync {
    2    public void
     acquire();
    3    public void
     release();
    4}

     1/**
     2
     * A simple Sempahore implementaion.
     3
     * 
     4
     * @author sakis
     5 */

     6public class Semaphore implements Sync{
     7    private int
     value;
     8

     9    
    /**
    10
         * Sempahore(1) is a Mutex.
    11     */

    12    public Semaphore() {
    13        value = 1
    ;
    14    }

    15
    16    public Semaphore(int value) 
    {
    17        this.value =
     value;
    18    }

    19
    20    
    /**
    21
         * P(): waits until the semaphore's value is greater than zero( there are
    22
         * enough resources to use), then decrements it.
    23     */

    24    synchronized public void acquire() {
    25        while (value <= 0
    {
    26            try 
    {
    27
                    wait();
    28            }
     catch (InterruptedException e) {
    29            }

    30        }

    31        --value;
    32    }

    33
    34    
    /**
    35
         * V(): increments the semaphore's value(release one resource), and wakes up
    36
         * threads waiting in P() if possible.
    37     */

    38    synchronized public void release() {
    39        ++
    value;
    40
            notify();
    41    }

    42}


     1/**
     2
     * 讀-寫鎖,讀者和寫者互斥,寫者之間互斥。
     3
     * Standard usage:
     4
     * class X {
     5
     *   ReadWriteLock rw;
     6
     *   // 
     7
     *   public void read() throws InterruptedException { 
     8
     *     rw.readLock().acquire();
     9
     *     try {
    10
     *       //  do the read
    11
     *     }
    12
     *     finally {
    13
     *       rw.readlock().release()
    14
     *     }
    15
     *   }
    16
     *
    17
     *   public void write() throws InterruptedException { 
    18
     *     rw.writeLock().acquire();
    19
     *     try {
    20
     *       //  do the write
    21
     *     }
    22
     *     finally {
    23
     *       rw.writelock().release()
    24
     *     }
    25
     *   }
    26
     * }
    27
     * @see NoPreferenceReadWriteLock
    28
     *      NoPreferenceReadWriteLockWithSemaphore
    29
     *         WriterPreferenceReadWriteLock
    30 */

    31public interface ReadWriteLock {
    32    public
     Sync readLock();
    33    public
     Sync writeLock();
    34}

    第一種讀寫鎖實(shí)現(xiàn),使用java的管程,reader和writer優(yōu)先級(jí)相同

     1/**
     2
     * WriteReadLock implementation, reader writer have same priority.
     3
     * @author sakis
     4 */

     5public class NoPreferenceReadWriteLock implements ReadWriteLock {
     6    private static int readCount = 0
    ;
     7    private static final int FREE = 0
    ;
     8    private static final int READING = 1
    ;
     9    private static final int WRITING = 2
    ;
    10    private int status =
     FREE;
    11    private final ReadLock readLock = new
     ReadLock();
    12    private final WriteLock writeLock = new
     WriteLock();
    13

    14    public Sync readLock() 
    {
    15        return
     readLock;
    16    }

    17
    18    public Sync writeLock() 
    {
    19        return
     writeLock;
    20    }

    21
    22    private class ReadLock implements Sync 
    {
    23        public void acquire() 
    {
    24
                beforeRead();
    25        }

    26
    27        public void release() 
    {
    28
                afterRead();
    29        }

    30    }

    31
    32    private class WriteLock implements Sync 
    {
    33        public void acquire() 
    {
    34
                beforeWrite();
    35        }

    36
    37        public void release() 
    {
    38
                afterWrite();
    39        }

    40    }

    41
    42    private synchronized void beforeRead() 
    {
    43        while (!canRead()) 
    {
    44            try 
    {
    45
                    wait();
    46            }
     catch (InterruptedException e) {
    47            }

    48        }

    49        // If I'm the first reader, mark the status as READING.
    50        if (++readCount == 1)
    51
                setStatus(READING);
    52    }

    53
    54    private synchronized void afterRead() 
    {
    55        //
     If I'm the last reader, mark the status as FREE.
    56        // and wake up one writer who waits on the database.

    57        if (--readCount == 0{
    58
                setStatus(FREE);
    59
                notify();
    60        }

    61    }

    62
    63    private synchronized void beforeWrite() 
    {
    64        // Wait until nobody is writing or reading the database.

    65        while (!canWrite()) {
    66            try 
    {
    67
                    wait();
    68            }
     catch (InterruptedException e) {
    69            }

    70        }

    71        // mark the status as WRITING.
    72        setStatus(WRITING);
    73    }

    74
    75    private synchronized void afterWrite() 
    {
    76        //
     After writing, mark the status as FREE.
    77        // and wake up all readers who waits on the database.

    78        setStatus(FREE);
    79
            notifyAll();
    80    }

    81
    82    private boolean canRead() 
    {
    83        return status == FREE || status ==
     READING;
    84    }

    85
    86    private boolean canWrite() 
    {
    87        return status ==
     FREE;
    88    }

    89
    90    private void setStatus(int status) 
    {
    91        this.status =
     status;
    92    }

    93
    94}


    第二種讀寫鎖實(shí)現(xiàn),使用信號(hào)量,reader和writer優(yōu)先級(jí)相同

     1/**
     2
     * WriteReadLock implementation using Semaphore, 
     3
     * reader writer have same priority.
     4
     * @author sakis
     5 */

     6public class NoPreferenceReadWriteLockWithSemaphore implements ReadWriteLock {
     7    private static int readCount = 0
    ;
     8    private Semaphore rcMutex = new
     Semaphore();
     9    private Semaphore dataMutex = new
     Semaphore();;
    10    private final ReadLock readLock = new
     ReadLock();
    11    private final WriteLock writeLock = new
     WriteLock();
    12

    13    public Sync readLock() 
    {
    14        return
     readLock;
    15    }

    16
    17    public Sync writeLock() 
    {
    18        return
     writeLock;
    19    }

    20
    21    private class ReadLock implements Sync 
    {
    22        public void acquire() 
    {
    23
                beforeRead();
    24        }

    25
    26        public void release() 
    {
    27
                afterRead();
    28        }

    29    }

    30
    31    private class WriteLock implements Sync 
    {
    32        public void acquire() 
    {
    33
                beforeWrite();
    34        }

    35
    36        public void release() 
    {
    37
                afterWrite();
    38        }

    39    }

    40
    41    private synchronized void beforeRead() 
    {
    42
            rcMutex.acquire();
    43        //
     If i'm the first reader, disallow writer
    44        // to access the database by dataMutex.P();

    45        if (++readCount == 1)
    46
                dataMutex.acquire();
    47
            rcMutex.release();
    48    }

    49
    50    private synchronized void afterRead() 
    {
    51
            rcMutex.acquire();
    52        //
     If I'm the last reader, re-allow writer
    53        // to access the database by dataMutex.V();

    54        if (--readCount == 0)
    55
                dataMutex.release();
    56
            rcMutex.release();
    57    }

    58
    59    private synchronized void beforeWrite() 
    {
    60        //
     disallow other readers,writers to access
    61        // the database.

    62        dataMutex.acquire();
    63    }

    64
    65    private synchronized void afterWrite() 
    {
    66
            dataMutex.release();
    67    }

    68}

    第三種讀寫鎖實(shí)現(xiàn),使用信號(hào)量,writer優(yōu)先級(jí)高。
    @see EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock
      1/**
      2
     * WriteReadLock implementation, writer has higher priority.
      3 */

      4public class WriterPreferenceReadWriteLock implements ReadWriteLock {
      5

      6    private long waitingReaders = 0// threads try to read

      7    private long waitingWriters = 0// threads try to write
      8
      9    private long activeReaders = 0// threads excuting read
     10    private long activeWriters = 0// threads excuting write, 0 or 1
     11
     12    private final ReadLock readLock = new ReadLock();
     13    private final WriteLock writeLock = new
     WriteLock();
     14

     15    
    /*
     16     * @see mis0204.tao.concurrent.ReadWriteLock#readLock()
     17     */

     18    public Sync readLock() {
     19        return
     readLock;
     20    }

     21
     22    
    /*
     23     * @see mis0204.tao.concurrent.ReadWriteLock#writeLOck()
     24     */

     25    public Sync writeLock() {
     26        return
     writeLock;
     27    }

     28
     29    private class ReadLock implements Sync 
    {
     30        public void acquire() 
    {
     31
                beforeRead();
     32        }

     33
     34        public void release() 
    {
     35
                afterRead();
     36        }

     37    }

     38
     39    private class WriteLock implements Sync 
    {
     40        public void acquire() 
    {
     41
                beforeWrite();
     42        }

     43
     44        public void release() 
    {
     45
                afterWrite();
     46        }

     47    }

     48
     49    private synchronized void beforeRead() 
    {
     50        ++
    waitingReaders;
     51        while (!canRead()) 
    {
     52            try 
    {
     53
                    wait();
     54            }
     catch (InterruptedException e) {
     55                --waitingReaders; // roll back state

     56                e.printStackTrace();
     57            }

     58        }

     59        --waitingReaders;
     60        ++
    activeReaders;
     61    }

     62
     63    private synchronized void afterRead() 
    {
     64        --
    activeReaders;
     65
            notifyAll();
     66    }

     67
     68    private synchronized void beforeWrite() 
    {
     69        ++
    waitingWriters;
     70        while (!canWrite()) 
    {
     71            try 
    {
     72
                    wait();
     73            }
     catch (InterruptedException e) {
     74                --waitingWriters; // roll back state

     75                e.printStackTrace();
     76            }

     77        }

     78        --waitingWriters;
     79        ++
    activeWriters;
     80    }

     81
     82    private synchronized void afterWrite() 
    {
     83        --
    activeWriters;
     84
            notifyAll();
     85    }

     86
     87    
    /**
     88
         * @return true if no writers are waiting/writing on the resource.
     89     */

     90    private boolean canRead() {
     91        return waitingWriters == 0 && activeWriters == 0
    ;
     92    }

     93
     94    
    /**
     95
         * @return true if no readers/writers are using the resouce.
     96     */

     97    private boolean canWrite() {
     98        return activeReaders == 0 && activeWriters == 0
    ;
     99    }

    100}


    end
    posted on 2005-05-19 09:54 Duran's technical life 閱讀(1047) 評(píng)論(0)  編輯  收藏 所屬分類: 技術(shù)積累
     
    主站蜘蛛池模板: 亚洲一区免费在线观看| 中文字幕亚洲精品资源网| 免费无码AV片在线观看软件| 本免费AV无码专区一区| 亚洲精品美女久久久久久久| 亚洲综合色一区二区三区小说| 国产日产亚洲系列| 国产一级理论免费版| 成人特黄a级毛片免费视频| 7m凹凸精品分类大全免费| 中文字幕在线免费视频| 免费看一级毛片在线观看精品视频| 亚洲伊人久久大香线蕉在观| 亚洲国产精品一区| 久久亚洲国产欧洲精品一| 国产日产亚洲系列最新| 亚洲第一区精品观看| 免费A级毛片无码A∨男男| 精品无码国产污污污免费| 久久久久久久久免费看无码| 91热久久免费精品99| 久久永久免费人妻精品下载| 黄色片免费在线观看| a国产成人免费视频| 中文字幕在线成人免费看| 黄 色一级 成 人网站免费| 国产成人无码免费网站| 一级毛片免费视频网站| www免费插插视频| www一区二区www免费| jizz日本免费| 美女网站在线观看视频免费的| 久久高潮一级毛片免费| 大地影院MV在线观看视频免费| 99视频在线免费观看| 久久er国产精品免费观看2| 少妇人妻偷人精品免费视频| 1000部拍拍拍18勿入免费凤凰福利| 亚洲电影免费观看| 欧美a级在线现免费观看| 免费高清在线爱做视频|