ReentrantLock的使用

package concurrent._ReentrantLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//一个关于Lock的最基本操作,lock.lock()与lock.unlock()
public class MyReentrantLock {

    private static Lock lock = new ReentrantLock();
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        MyReentrantLock mylock = new MyReentrantLock();

        Thread a = new Thread("A"){
            @Override
            public void run() {
                mylock.lockinsert(Thread.currentThread());
            }
        };
        a.start();
        Thread b = new Thread("B"){
            @Override
            public void run() {
                mylock.lockinsert(Thread.currentThread());
            }
        };
        b.start();
        Thread c = new Thread("C"){
            @Override
            public void run() {
                mylock.lockinsert(Thread.currentThread());
            }
        };
        c.start();

        //等待三个线程结束后输出count的值,可以使用一个闭锁
        Thread.sleep(1000);
        System.out.println(count);




    }

    public void lockinsert(Thread thread){
        lock.lock();
        //线程如果没有获得锁,就会阻塞在此处,获得锁之后才能后续操作

        try{
            System.out.println("线程" + thread.currentThread().getName() + "获得了锁");

            for(int i = 0 ; i < 1000 ; i ++){
                count++;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("线程" + thread.currentThread().getName() + "释放了锁");

            lock.unlock();
        }
    }



}

结果:

线程A获得了锁
线程A释放了锁
线程B获得了锁
线程B释放了锁
线程C获得了锁
线程C释放了锁
3000

 

 

 

trylock()

package concurrent._ReentrantLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//一个关于Lock的最基本操作,lock.lock()与lock.unlock()
public class MyReentrantLock1 {

    private static Lock lock = new ReentrantLock();
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        MyReentrantLock1 mylock = new MyReentrantLock1();

        Thread a = new Thread("A"){
            @Override
            public void run() {
                mylock.trylockinsert(Thread.currentThread());
            }
        };
        a.start();
        Thread b = new Thread("B"){
            @Override
            public void run() {
                mylock.trylockinsert(Thread.currentThread());
            }
        };
        b.start();
        Thread c = new Thread("C"){
            @Override
            public void run() {
                mylock.trylockinsert(Thread.currentThread());
            }
        };
        c.start();

        //等待三个线程结束后输出count的值,可以使用一个闭锁
        Thread.sleep(1000);
        System.out.println(count);




    }

    public void trylockinsert(Thread thread){
        if(lock.tryLock()){
            try{
                System.out.println("线程" + thread.currentThread().getName() + "已经尝试获得了锁");

                for(int i = 0 ; i < 1000 ; i ++){
                    count++;
                }

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                System.out.println("线程" + thread.currentThread().getName() + "释放了锁");

                lock.unlock();
            }
        }else{
            System.out.println("线程" + thread.currentThread().getName() + "尝试获得锁失败");

        }

    }



}

结果:

线程C尝试获得锁失败
线程A尝试获得锁失败
线程B已经尝试获得了锁
线程B释放了锁
1000

 

修改代码,使用tryLock(time,TimeUnit)

package concurrent._ReentrantLock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Demo3 {
    private static Lock lock = new ReentrantLock();
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Demo3 demo = new Demo3();

        Thread a = new Thread("A"){
            @Override
            public void run() {
                try {
                    demo.trylockinsert(Thread.currentThread());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        a.start();
        Thread b = new Thread("B"){
            @Override
            public void run() {
                try {
                    demo.trylockinsert(Thread.currentThread());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        b.start();
        Thread c = new Thread("C"){
            @Override
            public void run() {
                try {
                    demo.trylockinsert(Thread.currentThread());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        c.start();

        //一秒钟之后中断b,这个时候b应该还在等待获得锁
        Thread.sleep(1000);
        System.out.println("主线程尝试中断bThread");
        b.interrupt();


    }


    public void trylockinsert(Thread thread) throws InterruptedException {

        //线程如果没有获得锁,就会阻塞在此处,获得锁之后才能后续操作
        if(lock.tryLock(4, TimeUnit.SECONDS)){
            try{
                System.out.println("线程" + thread.currentThread().getName() + "获 得了锁");
                long now = System.currentTimeMillis();
                while(System.currentTimeMillis() - now < 5000){
                    //让线程在while循环里面空转5秒钟
                    
                }

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                System.out.println("线程" + thread.currentThread().getName() + "释 放了锁");

                lock.unlock();
            }
        }else{
            System.out.println("线程" + thread.currentThread().getName() + "放弃了对锁的获取");

        }

    }
}

结果显示:线程A获得锁,线程B和C等待锁的释放4秒钟,但是一秒钟后b被主线程中断,C在四秒钟后放弃了对锁的获取。

线程A获 得了锁
主线程尝试中断bThread
java.lang.InterruptedException
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireNanos(AbstractQueuedSynchronizer.java:936)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireNanos(AbstractQueuedSynchronizer.java:1247)
    at java.util.concurrent.locks.ReentrantLock.tryLock(ReentrantLock.java:442)
    at concurrent._ReentrantLock.Demo3.trylockinsert(Demo3.java:60)
    at concurrent._ReentrantLock.Demo3$2.run(Demo3.java:29)
线程C放弃了对锁的获取
线程A释 放了锁

 

posted @ 2018-11-23 11:03  式微胡不归  阅读(202)  评论(0编辑  收藏  举报