
- private static int sum = 0;
- private static Lock lock = new ReentrantLock();
- //private static TulingLock lock = new TulingLock();
-
- public static void main(String[] args) throws InterruptedException {
-
- for (int i = 0; i < 3; i++) {
- Thread thread = new Thread(()->{
- //加锁
- lock.lock();
- try {
- // 临界区代码
- // TODO 业务逻辑:读写操作不能保证线程安全
- for (int j = 0; j < 10000; j++) {
- sum++;
- }
- } finally {
- // 解锁
- lock.unlock();
- }
- });
- thread.start();
- }
-
- Thread.sleep(2000);
- System.out.println(sum);
- }
- public static ReentrantLock lock = new ReentrantLock();
-
- public static void main(String[] args) {
- method1();
- }
-
-
- public static void method1() {
- lock.lock();
- try {
- log.debug("execute method1");
- method2();
- } finally {
- lock.unlock();
- }
- }
- public static void method2() {
- lock.lock();
- try {
- log.debug("execute method2");
- method3();
- } finally {
- lock.unlock();
- }
- }
- public static void method3() {
- lock.lock();
- try {
- log.debug("execute method3");
- } finally {
- lock.unlock();
- }
- }
- ReentrantLock lock = new ReentrantLock();
-
- Thread t1 = new Thread(() -> {
-
- log.debug("t1启动...");
-
- try {
- lock.lockInterruptibly();
- try {
- log.debug("t1获得了锁");
- } finally {
- lock.unlock();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- log.debug("t1等锁的过程中被中断");
- }
-
- }, "t1");
-
-
- lock.lock();
- try {
- log.debug("main线程获得了锁");
- t1.start();
- //先让线程t1执行
- Thread.sleep(1000);
-
- t1.interrupt();
- log.debug("线程t1执行中断");
- } finally {
- lock.unlock();
- }
- ReentrantLock lock = new ReentrantLock();
-
- Thread t1 = new Thread(() -> {
-
- log.debug("t1启动...");
- // 注意: 即使是设置的公平锁,此方法也会立即返回获取锁成功或失败,公平策略不生效
- // if (!lock.tryLock()) {
- // log.debug("t1获取锁失败,立即返回false");
- // return;
- // }
-
- //超时
- try {
- if (!lock.tryLock(1, TimeUnit.SECONDS)) {
- log.debug("等待 1s 后获取锁失败,返回");
- return;
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- return;
- }
-
- try {
- log.debug("t1获得了锁");
- } finally {
- lock.unlock();
- }
-
- }, "t1");
-
-
- lock.lock();
- try {
- log.debug("main线程获得了锁");
- t1.start();
- //先让线程t1执行
- Thread.sleep(2000);
- } finally {
- lock.unlock();
- }
- //ReentrantLock lock = new ReentrantLock(true); //公平锁
- ReentrantLock lock = new ReentrantLock(); //非公平锁
-
- for (int i = 0; i < 500; i++) {
- new Thread(() -> {
- lock.lock();
- try {
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- log.debug(Thread.currentThread().getName() + " running...");
- } finally {
- lock.unlock();
- }
- }, "t" + i).start();
- }
- // 1s 之后去争抢锁
- Thread.sleep(1000);
-
- for (int i = 0; i < 500; i++) {
- new Thread(() -> {
- lock.lock();
- try {
- log.debug(Thread.currentThread().getName() + " running...");
- } finally {
- lock.unlock();
- }
- }, "强行插入" + i).start();
-
- }
- private static ReentrantLock lock = new ReentrantLock();
- private static Condition cigCon = lock.newCondition();
- private static Condition takeCon = lock.newCondition();
-
- private static boolean hashcig = false;
- private static boolean hastakeout = false;
-
- //送烟
- public void cigratee(){
- lock.lock();
- try {
- while(!hashcig){
- try {
- log.debug("没有烟,歇一会");
- cigCon.await();
-
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- log.debug("有烟了,干活");
- }finally {
- lock.unlock();
- }
- }
-
- //送外卖
- public void takeout(){
- lock.lock();
- try {
- while(!hastakeout){
- try {
- log.debug("没有饭,歇一会");
- takeCon.await();
-
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- log.debug("有饭了,干活");
- }finally {
- lock.unlock();
- }
- }
-
- public static void main(String[] args) {
- ReentrantLockDemo6 test = new ReentrantLockDemo6();
- new Thread(() ->{
- test.cigratee();
- }).start();
-
- new Thread(() -> {
- test.takeout();
- }).start();
-
- new Thread(() ->{
- lock.lock();
- try {
- hashcig = true;
- log.debug("唤醒送烟的等待线程");
- cigCon.signal();
- }finally {
- lock.unlock();
- }
-
-
- },"t1").start();
-
- new Thread(() ->{
- lock.lock();
- try {
- hastakeout = true;
- log.debug("唤醒送饭的等待线程");
- takeCon.signal();
- }finally {
- lock.unlock();
- }
-
-
- },"t2").start();
- }