当前位置: 首页 > article >正文

【Java并发编程九】同步控制

ReentrantLock(重入锁)

ReentrantLock的基本使用

 ReentrantLock可以自己决定加锁的位置和解锁的位置。

package myTest;

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

public class myTest implements Runnable{
    // 重入锁
    public static ReentrantLock lock = new ReentrantLock();

    public static int i;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new myTest(), "Thread1");
        Thread t2 = new Thread(new myTest(), "Thread2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(i);
    }

    @Override
    public void run() {
        lock.lock();
        for (int j = 0; j < 10000; j++) {
            lock.lock();
            try {
                i ++;
            } finally {
                lock.unlock();
            }
        }
        lock.unlock();
        System.out.println(Thread.currentThread().getName());
    }
}
/**
Thread1
Thread2
20000
输出的结果总是上述形式。这是因为Thread1有一个锁是执行完所有循环才会释放。
*/

ReentrantLock.lockInterruptibly() 可中断锁

 如下面代码所示,如果使用ReentrantLock的lock()方法,则会造成死锁。但是使用了lockInterruptibly可中断锁之后,t2在等待2s之后请求另外一个锁没有得到之后,就会不再尝试去获取这个锁,设置中断标志,直接断开。

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;
public class myTest implements Runnable{
    // 重入锁
    public static ReentrantLock lock1 = new ReentrantLock();
    public static ReentrantLock lock2 = new ReentrantLock();
    int lock;
    public myTest(int lock) {
        this.lock = lock;
    }
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new myTest(1),"Thread1");
        Thread t2 = new Thread(new myTest(2),"Thread2");
        t1.start();
        t2.start();
        Thread.sleep(2000);
        t2.interrupt();
    }
    @Override
    public void run() {
        try {
            if(lock == 1) {
                lock1.lockInterruptibly();                System.out.println(Thread.currentThread().getName()+"开始运行");
                Thread.sleep(500);
                lock2.lockInterruptibly();
            } else {
                lock2.lockInterruptibly();                System.out.println(Thread.currentThread().getName()+"开始运行");
                Thread.sleep(500);
                lock1.lockInterruptibly();
            }
        } catch (InterruptedException e) {                System.out.println(Thread.currentThread().getName()+"运行出错!");
//                throw new RuntimeException(e);
        } finally {
            if(lock1.isHeldByCurrentThread()) {
                lock1.unlock();
            }
            if (lock2.isHeldByCurrentThread()) {
                lock2.unlock();
            }            System.out.println(Thread.currentThread().getName()+"退出");
        }
    }
}

ReentrantLock.tryLock() 限时等待锁

 使用中断锁虽然可以解决死锁,但是很出现数据不一致的问题。可以使用tryLock() 去等待锁,如果时间到了还没有获得锁,便直接放弃。
 如下面代码所示,执行慢的线程,在等待5s后,会直接执行else里面的代码。

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

public class myTest implements Runnable{
    // 重入锁
    public static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new myTest(),"Thread1");
        Thread t2 = new Thread(new myTest(),"Thread2");
        t1.start();
        t2.start();
    }
    @Override
    public void run() {
        try {
            if(lock.tryLock(2, TimeUnit.SECONDS)) {
                Thread.sleep(3000);
            } else {
                System.out.println(Thread.currentThread().getName()+"得到锁失败");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            if(lock.isHeldByCurrentThread()) {
                System.out.println(Thread.currentThread().getName()+"解锁");
                lock.unlock();
            }
        }
    }
}

公平锁和非公平锁

 ReentrantLock() 默认是非公平锁,默认值为false,两个线程不会公平的获得锁。
 ReentrantLock(true) 为公平锁,两个线程会公平地获取锁,如下面代码所示,两个线程会依次执行。

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class myTest implements Runnable{
    // 重入锁
    public static ReentrantLock lock = new ReentrantLock(true);

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new myTest(),"Thread1");
        Thread t2 = new Thread(new myTest(),"Thread2");
        t1.start();
        t2.start();
    }
    @Override
    public void run() {
        while(true) {
            try {
                lock.lock();
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName()+"获得锁");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }
}

http://www.kler.cn/news/135672.html

相关文章:

  • Redis-核心数据结构
  • 【C/PTA】数组进阶练习(一)
  • 4.Spring IoC 的实现机制是什么?
  • Bean实例化的基本流程
  • 色彩的基础知识——适用于camera tuning
  • Failed to execute org.scala-tools:maven-scala-plugin:2.15.2解决
  • 携带二进制文件的软件恢复方法
  • ZC序列理论学习及仿真
  • 【人工智能时代的刑法体系与责任主体概述】
  • 数据结构:枚举
  • 图片降噪软件 Topaz DeNoise AI mac中文版功能
  • Chrome添加扩展程序
  • [AI]ChatGPT4 与 ChatGPT3.5 区别有多大
  • 重要功能丨支持1688API接口接入一键跨境铺货及采购,解决跨境卖家货源烦恼!
  • 【数据结构初阶】双链表
  • 多因素方差分析(Multi-way Analysis of Variance) R实现
  • 栈和队列知识点+例题
  • 中国农业开启加速度,龙江农业迎来黄金期
  • 二阶段提交
  • thonny的汉字编码是UTF-8,如何才能转为GB2312?
  • LeetCode - 622. 设计循环队列(C语言,顺序存储结构,配图)
  • 逻辑漏洞(越权)
  • MySQL 的执行原理(五)
  • AI 游戏工具收集
  • 设计模式(二)-创建者模式(1)-单例模式
  • uniapp Android如何授权打开系统蓝牙Bluetooth?
  • 【ES6标准入门】JavaScript中的模块Module语法的使用细节:export命令和imprt命令详细使用,超级详细!!!
  • vue统一登录
  • C# 依赖注入如何实现
  • 数据结构【DS】栈