超时
中断;
生产者/消费者队列

 

public class Demo09 implements Runnable {
  public static ReentrantLock lock = new ReentrantLock(); //锁 相当于一份公共资源
  public static void main(String[] args) {
    Thread t1 = new Thread(new Demo09());
    Thread t2 = new Thread(new Demo09());
    t1.start();
    t2.start();
  }

  @Override
  public void run() {
    lock.lock(); // 获取锁
    for(int j = 0; j < 3; j++) {
      System.out.println(Thread.currentThread().getName() + "\t" + j);
    }
    lock.unlock(); // 释放锁
  }
}

 

https://www.cnblogs.com/DDiamondd/p/11315978.html

相关文章:

  • 2021-07-26
  • 2021-09-21
  • 2021-04-08
  • 2021-06-14
  • 2021-09-22
  • 2021-10-23
  • 2021-06-06
  • 2021-07-12
猜你喜欢
  • 2022-12-23
  • 2022-01-04
  • 2021-11-20
  • 2022-01-24
  • 2022-12-23
  • 2021-08-25
  • 2021-11-23
相关资源
相似解决方案