多线程之ReenteantLook

多线程之ReenteantLook

package com.liu.demo07;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @outhor liu
 * @dare 2020/7/7 23:22
 */
public class AReenteantLook implements Runnable{
    public static ReentrantLock lock = new ReentrantLock();
    public static int i = 0;
    @Override
    public void run() {
        for (int i = 0; i <5 ; i++) {
            //加锁
            lock.lock();
           // lock.lock();
            i++;
            lock.unlock();
            //释放锁
          //  lock.unlock();
        }
    }
    public static void main(String[] args) throws InterruptedException {
        AReenteantLook aReenteantLook = new AReenteantLook();
        Thread thread = new Thread(aReenteantLook);
        thread.start();
        thread.join();
        System.out.println(i);
    }
}

输出:

0
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
	at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
	at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457)
	at com.liu.demo07.AReenteantLook.run(AReenteantLook.java:21)
	at java.lang.Thread.run(Thread.java:748)
package com.liu.demo07;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @outhor liu
 * @dare 2020/7/7 23:33
 */
public class BReenteantLook {
    //锁1
    public ReentrantLock locka = new ReentrantLock();
    //锁2
    public ReentrantLock lockb = new ReentrantLock();

    public Thread locka(){
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //线程未被中断
                    locka.lockInterruptibly();
                    //具体业务
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    lockb.lockInterruptibly();
                    System.out.println(Thread.currentThread().getName()+ ",执行完毕");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    //如果还持有该锁。则释放该锁
                    if (locka.isHeldByCurrentThread()){
                        locka.unlock();
                    }
                    if (lockb.isHeldByCurrentThread()){
                        lockb.unlock();
                    }
                    System.out.println(Thread.currentThread().getName()+ ",退出");
                }
            }
        });
        t.start();
        return t;
    }
    public Thread lockb(){
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lockb.lockInterruptibly();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    locka.lockInterruptibly();
                    System.out.println(Thread.currentThread().getName()+ ",执行完毕");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    if (locka.isHeldByCurrentThread()){
                        locka.unlock();
                    }if (lockb.isHeldByCurrentThread()){
                        lockb.unlock();
                    }
                    System.out.println(Thread.currentThread().getName()+ ",退出");
                }
            }
        });
        t.start();
        return t;
    }

    public static void main(String[] args) {
        long timeMillis = System.currentTimeMillis();
        BReenteantLook bReenteantLook = new BReenteantLook();
        Thread locka = bReenteantLook.locka();
        Thread lockb = bReenteantLook.lockb();
        //自旋一段时间,如果等待时间过长,可能会发生死锁,那么就自己干掉自己
        while (true){
            if (System.currentTimeMillis() - timeMillis >= 2000){
                lockb.interrupt();
            }
        }
    }
}

输出:

java.lang.InterruptedException
Thread-1,退出
Thread-0,执行完毕
Thread-0,退出
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:898)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1222)
	at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
	at com.liu.demo07.BReenteantLook$2.run(BReenteantLook.java:58)
	at java.lang.Thread.run(Thread.java:748)

你可能感兴趣的:(多线程之ReenteantLook)