ReentrantLock简单实现2

ReentrantLock:

  

 1 /**
 2  *    ReentrantLock测试逻辑类
 3  */
 4 public class MyService {
 5     private Lock lock = new ReentrantLock();//获取锁对象
 6     
 7     public void methodA() {
 8         try {
 9             lock.lock();
10             System.out.println("methodA:----begin" + Thread.currentThread().getName() + "----" + System.currentTimeMillis());
11             Thread.sleep(2000);
12             System.out.println("methodA:----end" + Thread.currentThread().getName() + "----" + System.currentTimeMillis());
13         } catch (InterruptedException e) {
14             e.printStackTrace();
15         }finally {
16             lock.unlock();
17         }
18     }
19     
20     public void methodB() {
21         try {
22             lock.lock();
23             System.out.println("methodB:----begin" + Thread.currentThread().getName() + "----" + System.currentTimeMillis());
24             Thread.sleep(2000);
25             System.out.println("methodB:----end" + Thread.currentThread().getName() + "----" + System.currentTimeMillis());
26         } catch (InterruptedException e) {
27             e.printStackTrace();
28         }finally {
29             lock.unlock();
30         }
31     }
32 }
 1 /**
 2  *    线程A
 3  */
 4 public class ThreadA extends Thread {
 5     
 6     private MyService service;
 7     
 8     public ThreadA(MyService service) {
 9         this.service = service;
10     }
11     
12     @Override
13     public void run() {
14         service.methodA();
15     }
16 }
 1 /**
 2  *    线程AA
 3  */
 4 public class ThreadAA extends Thread {
 5     
 6     private MyService service;
 7     
 8     public ThreadAA(MyService service) {
 9         this.service = service;
10     }
11     
12     @Override
13     public void run() {
14         service.methodA();
15     }
16 }
 1 /**
 2  *    线程B
 3  */
 4 public class ThreadB extends Thread {
 5     
 6     private MyService service;
 7     
 8     public ThreadB(MyService service) {
 9         this.service = service;
10     }
11     
12     @Override
13     public void run() {
14         service.methodB();
15     }
16 }
 1 /**
 2  *    线程BB
 3  */
 4 public class ThreadBB extends Thread {
 5     
 6     private MyService service;
 7     
 8     public ThreadBB(MyService service) {
 9         this.service = service;
10     }
11     
12     @Override
13     public void run() {
14         service.methodB();
15     }
16 }
 1 /**
 2  *    测试类
 3  */
 4 public class Run {
 5 
 6     public static void main(String[] args) {
 7         MyService service = new MyService();
 8         
 9         ThreadA a = new ThreadA(service);
10         a.setName("A");
11         a.start();
12         ThreadAA aa = new ThreadAA(service);
13         aa.setName("AA");
14         aa.start();
15 
16         ThreadB b = new ThreadB(service);
17         b.setName("B");
18         b.start();
19         ThreadBB bb = new ThreadBB(service);
20         bb.setName("BB");
21         bb.start();
22     }
23 }

运行结果如下:

  

 

转载于:https://www.cnblogs.com/wang1001/p/9566640.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你实现一个基于ReentrantLock和Condition的简单阻塞队列。 首先,我们需要定义一个队列类,包含以下方法: - `enqueue(item)`:将元素放入队列中。 - `dequeue()`:从队列中取出元素。 - `size()`:返回队列的大小。 下面是使用ReentrantLock和Condition实现的代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class BlockingQueue<T> { private Queue<T> queue; private int maxSize; private ReentrantLock lock; private Condition notEmpty; private Condition notFull; public BlockingQueue(int maxSize) { this.maxSize = maxSize; this.queue = new LinkedList<>(); this.lock = new ReentrantLock(); this.notEmpty = lock.newCondition(); this.notFull = lock.newCondition(); } public void enqueue(T item) throws InterruptedException { lock.lock(); try { while (queue.size() == maxSize) { // 队列已满,等待直到队列不满 notFull.await(); } queue.add(item); notEmpty.signalAll(); // 通知等待中的线程队列不为空 } finally { lock.unlock(); } } public T dequeue() throws InterruptedException { lock.lock(); try { while (queue.isEmpty()) { // 队列为空,等待直到队列不为空 notEmpty.await(); } T item = queue.remove(); notFull.signalAll(); // 通知等待中的线程队列不满 return item; } finally { lock.unlock(); } } public int size() { lock.lock(); try { return queue.size(); } finally { lock.unlock(); } } } ``` 使用这个阻塞队列的示例代码如下: ```java public class Main { public static void main(String[] args) { BlockingQueue<Integer> queue = new BlockingQueue<>(5); Thread producer = new Thread(() -> { for (int i = 0; i < 10; i++) { try { queue.enqueue(i); System.out.println("Produced: " + i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread consumer = new Thread(() -> { for (int i = 0; i < 10; i++) { try { int item = queue.dequeue(); System.out.println("Consumed: " + item); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); producer.start(); consumer.start(); } } ``` 在这个示例中,生产者线程不断地将元素放入队列中,消费者线程不断地从队列中取出元素。当队列已满时,生产者线程会被阻塞,直到有空余位置;当队列为空时,消费者线程会被阻塞,直到有元素可取。 希望这个简单的阻塞队列的实现对你有帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值