java多线程Java 杂谈multiThread

Java 多线程(六):ReentrantLock 与 Cond

2018-05-10  本文已影响95人  聪明的奇瑞

ReentrantLock

public interface Lock {
    void lock();//获得锁,如果锁已经被占用则等待,必须等待当前线程结束才会响应其它线程的中断
    void lockInterruptibly() throws InterruptedException;//获得锁,但如果检测到 interrupt 标志为 true 则立刻抛出 InterruptedException 异常
    boolean tryLock();// 尝试获得锁,如果成功,返回true,失败返回false。该方法不等待,立即返回
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException; //   在给定时间内尝试获得锁
    void unlock();   //用来释放锁
    Condition newCondition();
}

用法

public class LockTest implements Runnable{

    public static ReentrantLock reentrantLock = new ReentrantLock();

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getId()+"进入方法");
        try {
            // reentrantLock.lockInterruptibly();  检测到 interrupt 标志则立刻抛出异常
            reentrantLock.lock();
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getId()+"执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) {
        LockTest lockTest = new LockTest();
        new Thread(lockTest).start();
        new Thread(lockTest).start();
    }

}

公平锁和非公平锁

// 构造参数为 true 为公平锁(默认),false 为非公平锁
public static ReentrantLock reentrantLock = new ReentrantLock( false);

Condition

void await() throws InterruptedException; // 使当前线程等待,同时释放锁,当其它线程中使用 singal() 和 signalAll() 方法时,线程会重新获得锁并继续执行,或者当前线程中断时也跳出等待
void awaitUninterruptibly();    // 与 await() 方法基本相同,但不会响应中断
void signal();  // 唤醒一个在等待的线程
void signalAll();   // 唤醒所有在等待的线程

用法

public class LockTest implements Runnable{

    public static ReentrantLock reentrantLock = new ReentrantLock( );
    public static Condition condition = reentrantLock.newCondition();

    public Boolean flag;

    public LockTest(Boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getId()+"进入方法");
        try {
            reentrantLock.lock();
            System.out.println(Thread.currentThread().getId()+"执行中");
            if(flag)
                condition.signal();
            else
                condition.await();
            System.out.println(Thread.currentThread().getId()+"执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) {
        LockTest l1 = new LockTest(false)
        new Thread(l1).start();
        LockTest l2 = new LockTest(true);
        new Thread(l2).start();
    }

}

上一篇下一篇

猜你喜欢

热点阅读