Java并发Lock接口

java.util.concurrent.locks.Lock接口用作线程同步机制,类似于同步块。新的锁定机制更灵活,提供比同步块更多的选项。 锁和同步块之间的主要区别如下:

  • 序列的保证 - 同步块不提供对等待线程进行访问的序列的任何保证,但Lock接口处理它。
  • 无超时,如果未授予锁,则同步块没有超时选项。Lock接口提供了这样的选项。
  • 单一方法同步块必须完全包含在单个方法中,而Lock接口的方法lock()unlock()可以以不同的方式调用。

Lock类中的方法

以下是Lock类中可用的重要方法的列表。

编号方法描述说明
1public void lock()获得锁
2public void lockInterruptibly()获取锁定,除非当前线程中断
3public Condition newCondition()返回绑定到此Lock实例的新Condition实例
4public boolean tryLock()只有在调用时才可以获得锁
5public boolean tryLock(long time, TimeUnit unit)如果在给定的等待时间内自由,并且当前线程未被中断,则获取该锁。
6public void unlock()释放锁

示例

以下TestThread程序演示了使用Lock接口的一些方法。 这里我们使用lock()获取锁和unlock()来释放锁。

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

class PrintDemo {

   private final Lock queueLock = new ReentrantLock();

   public void print() {
      queueLock.lock();
      try {
         Long duration = (long) (Math.random() * 10000);
         System.out.println(Thread.currentThread().getName() 
            + "  Time Taken " + (duration / 1000) + " seconds.");
         Thread.sleep(duration);
      } catch (InterruptedException e) {
         e.printStackTrace();
      } finally {
         System.out.printf("%s printed the document successfully.\n", Thread.currentThread().getName());
         queueLock.unlock();
      }
   }
}

class ThreadDemo extends Thread {
   PrintDemo  printDemo;

   ThreadDemo( String name,  PrintDemo printDemo) {
      super(name);
      this.printDemo = printDemo;
   }   

   @Override
   public void run() {
      System.out.printf("%s starts printing a document\n", Thread.currentThread().getName());
      printDemo.print();
   }
}

public class TestThread {

   public static void main(String args[]) {
      PrintDemo PD = new PrintDemo();

      ThreadDemo t1 = new ThreadDemo( "Thread - 1 ", PD );
      ThreadDemo t2 = new ThreadDemo( "Thread - 2 ", PD );
      ThreadDemo t3 = new ThreadDemo( "Thread - 3 ", PD );
      ThreadDemo t4 = new ThreadDemo( "Thread - 4 ", PD );

      t1.start();
      t2.start();
      t3.start();
      t4.start();
   }
}

Java

执行上面示例代码,得到以下结果 -

Thread - 1  starts printing a document
Thread - 4  starts printing a document
Thread - 3  starts printing a document
Thread - 2  starts printing a document
Thread - 1   Time Taken 4 seconds.
Thread - 1  printed the document successfully.
Thread - 4   Time Taken 3 seconds.
Thread - 4  printed the document successfully.
Thread - 3   Time Taken 5 seconds.
Thread - 3  printed the document successfully.
Thread - 2   Time Taken 4 seconds.
Thread - 2  printed the document successfully.

Java

在上面的示例中,使用ReentrantLock类作为Lock接口的一个实现。 ReentrantLock类允许线程锁定方法,即使它已经具有其他方法锁。

  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧浩海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值