lock接口认识及可重入锁的实现

 Lock接口中有lockInterruptibly方法(线程中断),tryLock方法(获取锁成功返回成功,获取锁失败返回失败),还有就是锁的公平性问题,还有就是提供读写性能的ReadWriteLock等等。

 Lock需要显示地获取和释放锁,繁琐,能让代码更加灵活
 Synchronized不需要显示地获取和释放锁,简单。
 使用Lock可以方便的实现公平性
 非阻塞的获取锁
 能被中断的获取锁
 超时获取锁
 1 public class Sequence {
 2     
 3     private int value;
 4     
 5     Lock lock = new ReentrantLock();
 6     
 7     
 8     public int getNext() {
 9         lock.lock();
10         int a = value ++;
11         lock.unlock();
12         return a;
13     }
14     
15     
16     public static void main(String[] args) {
17         Sequence s = new Sequence();
18         new Thread(new Runnable() {
19             @Override
20             public void run() {
21                 while(true) {
22                     System.out.println(Thread.currentThread().getName()+ " "+s.getNext());
23                     
24                     try {
25                         Thread.sleep(100);
26                     } catch (InterruptedException e) {
27                         // TODO Auto-generated catch block
28                         e.printStackTrace();
29                     }
30                 }
31             }
32         }).start();
33     
34         
35         new Thread(new Runnable() {
36             @Override
37             public void run() {
38                 while(true) {
39                     System.out.println(Thread.currentThread().getName()+ " "+s.getNext());
40                     
41                     try {
42                         Thread.sleep(100);
43                     } catch (InterruptedException e) {
44                         // TODO Auto-generated catch block
45                         e.printStackTrace();
46                     }
47                 }
48             }
49         }).start();
50     }
51 
52 }

2,可重入锁的实现

//Demo.java

public
class Demo { MyLock lock = new MyLock(); public void a() { lock.lock(); //1 System.out.println("a"); b(); lock.unlock(); //4 } public void b() { lock.lock(); //2 System.out.println("b"); lock.unlock(); //3 } public static void main(String[] args) { Demo d = new Demo(); new Thread(new Runnable() { @Override public void run() { d.a(); } }).start(); } }
 1 public class MyLock implements Lock {
 2 
 3     private boolean isLocked = false;
 4     
 5     private Thread lockBy = null;
 6     
 7     int lockCount = 0;
 8     
 9     @Override
10     public synchronized void lock() {
11         
12         Thread currentThread = Thread.currentThread(); //1,第一个线程Thread-0 islocked = false 
13         //2 thread-0再次进入lock方法 当前线程还是thread-0
14         while(isLocked && currentThread != lockBy) //1,跳过 2,islock=true ,currentThread = lockby,跳过
15         try {
16             wait();
17         } catch (InterruptedException e) {
18             e.printStackTrace();
19         }
20         isLocked = true;     //1
21         lockBy = currentThread; //1把当前线程指定为lockby
22         lockCount ++; //1 lockCount=1    
23         //2 lockCount =2
24         
25     }
26     
27     
28 
29     @Override
30     public synchronized void unlock() {
31         if(lockBy == Thread.currentThread()) {
32             lockCount --;  // 3 ,lockcount = 1
33             //4 lockcount = 0
34             if(lockCount == 0 ) {
35                 notify();        //4
36                 isLocked =false; //4
37             }
38         }
39 
40     }
41 
42     
43     
44     @Override
45     public void lockInterruptibly() throws InterruptedException {
46         // TODO Auto-generated method stub
47         
48     }
49 
50     @Override
51     public boolean tryLock() {
52         // TODO Auto-generated method stub
53         return false;
54     }
55 
56     @Override
57     public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
58         // TODO Auto-generated method stub
59         return false;
60     }
61 
62 
63     @Override
64     public Condition newCondition() {
65         // TODO Auto-generated method stub
66         return null;
67     }
68 
69 }

 

posted @ 2019-02-14 09:42  曲阳阳  阅读(460)  评论(0编辑  收藏  举报