码迷,mamicode.com
首页 > 编程语言 > 详细

*多线程锁实现类

时间:2016-09-18 11:52:13      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

  1 import android.os.SystemClock;
  2 public class ConditionVariable
  3 {
  4     private volatile boolean mCondition;
  5     public ConditionVariable()
  6     {
  7         mCondition = false;
  8     }
  9     public ConditionVariable(boolean state)
 10     {
 11         mCondition = state;
 12     }
 13     public void unlock()
 14     {
 15         synchronized (this)
 16         {
 17             boolean old = mCondition;
 18             mCondition = true;
 19             if (!old)
 20                 this.notifyAll();
 21         }
 22     }
 23     public void lock()
 24     {
 25         synchronized (this)
 26         {
 27             boolean old = mCondition;
 28             mCondition = false;
 29             if (old)
 30                 this.notifyAll();
 31         }
 32     }
 33     /**
 34      * 阻塞 直到 unlock
 35      */
 36     public void blockIflock()
 37     {
 38         synchronized (this)
 39         {
 40             while (!mCondition)
 41             {
 42                 try
 43                 {
 44                     this.wait();
 45                 } catch (InterruptedException e)
 46                 {
 47                 }
 48             }
 49         }
 50     }
 51     /**
 52      * 阻塞 直到 unlock 或 timeout
 53      * 
 54      * @param timeout
 55      * @return isTimeout
 56      */
 57     public boolean blockIflock(long timeout)
 58     {
 59         if (timeout != 0)
 60         {
 61             synchronized (this)
 62             {
 63                 long now = SystemClock.elapsedRealtime();
 64                 long end = now + timeout;
 65                 while (!mCondition && now < end)
 66                 {
 67                     try
 68                     {
 69                         this.wait(end - now);
 70                     } catch (InterruptedException e)
 71                     {
 72                     }
 73                     now = SystemClock.elapsedRealtime();
 74                 }
 75                 return !mCondition;
 76             }
 77         } else
 78         {
 79             this.blockIflock();
 80             return false;
 81         }
 82     }
 83     /**
 84      * 阻塞 直到 lock
 85      */
 86     public void blockIfUnlock()
 87     {
 88         synchronized (this)
 89         {
 90             while (mCondition)
 91             {
 92                 try
 93                 {
 94                     this.wait();
 95                 } catch (InterruptedException e)
 96                 {
 97                 }
 98             }
 99         }
100     }
101     /**
102      * 阻塞 直到 lock 或 timeout
103      * 
104      * @param timeout
105      * @return isTimeout
106      */
107     public boolean blockIfUnlock(long timeout)
108     {
109         if (timeout != 0)
110         {
111             synchronized (this)
112             {
113                 long now = SystemClock.elapsedRealtime();
114                 long end = now + timeout;
115                 while (mCondition && now < end)
116                 {
117                     try
118                     {
119                         this.wait(end - now);
120                     } catch (InterruptedException e)
121                     {
122                     }
123                     now = SystemClock.elapsedRealtime();
124                 }
125                 return mCondition;
126             }
127         } else
128         {
129             this.blockIfUnlock();
130             return false;
131         }
132     }
133     public boolean isUnlock()
134     {
135         return mCondition;
136     }
137 }

 

*多线程锁实现类

标签:

原文地址:http://www.cnblogs.com/HellcNQB/p/5880838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!