<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ù)積累
     
    主站蜘蛛池模板: 毛片基地免费观看| 99视频在线免费| 国产v片免费播放| 亚洲日韩AV一区二区三区中文| 最近最好最新2019中文字幕免费| 亚洲AV中文无码字幕色三| 中文字幕在线免费观看视频| 在线A亚洲老鸭窝天堂| 中文在线免费视频| 亚洲国产精品国自产拍AV| 99久久99久久免费精品小说| 亚洲欧洲春色校园另类小说| 欧洲黑大粗无码免费| 亚洲欧美日韩国产精品一区| 免费观看国产精品| 一边摸一边桶一边脱免费视频| 亚洲日韩欧洲无码av夜夜摸| 国产无遮挡无码视频免费软件| 亚洲黄色中文字幕| 最近最好的中文字幕2019免费| 国产亚洲视频在线播放大全| 中文字幕亚洲一区| 国产精品免费福利久久| 亚洲国产精品乱码在线观看97| 成人免费777777| 久青草国产免费观看| 亚洲网站在线免费观看| 免费涩涩在线视频网| 91av免费在线视频| 久久久久亚洲精品日久生情 | 久久亚洲精品无码VA大香大香| 国产福利视精品永久免费| 亚洲国产精品成人AV在线| 亚洲人妻av伦理| 精品成在人线AV无码免费看| 亚洲 欧洲 视频 伦小说| 丁香五月亚洲综合深深爱| 日日麻批免费40分钟日本的| 国产亚洲精品美女| 久久久久亚洲AV无码专区首JN| 免费一区二区三区四区五区|