public class ReenterLock implements Runnable{
public static ReentrantLock lock=new ReentrantLock();
public static int i=0;
@Override
public void run() {
for(int j=0;j<10000000;j++){
lock.lock();
try{
i++;
}finally{
lock.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
ReenterLock tl=new ReenterLock();
Thread t1=new Thread(tl);
Thread t2=new Thread(tl);
t1.start();t2.start();
t1.join();t2.join();
System.out.println(i);
}
}

重用lock:

 public class ReenterLock2 implements Runnable{
public static ReentrantLock lock=new ReentrantLock();
public static int i=0;
@Override
public void run() {
for(int j=0;j<10000000;j++){
lock.lock();
lock.lock();
try{
i++;
}finally{
lock.unlock();
lock.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
ReenterLock2 tl=new ReenterLock2();
Thread t1=new Thread(tl);
Thread t2=new Thread(tl);
t1.start();t2.start();
t1.join();t2.join();
System.out.println(i);
}
}

最新文章

  1. jquery选择器使用说明
  2. Java、Hibernate(JPA)注解大全
  3. 20160314 Servlet 入门
  4. bing---iis how to process http request
  5. 最最常用的 100 个 Java 类(转)
  6. Linq to Sqlite连接
  7. POJ 1021 2D-Nim
  8. 90. Subsets II(中等,编写代码有难度)
  9. ZooKeeper之(四)配置与命令
  10. Scala变量| 流程控制
  11. 一对多关联模型,BELONGS_TO
  12. python操作pymysql数据库
  13. angular.copy()
  14. Golang标准库——io-结构
  15. Ansible系列(四):playbook应用和roles自动化批量安装示例
  16. JavaScript简易教程
  17. LR-事务
  18. netstat命令初探
  19. xdebug的安装测试
  20. 第一次软件工程作业补充plus

热门文章

  1. C#压缩和解压文件
  2. Python windows环境 搭建问题
  3. kvm第三章--虚拟化存储管理
  4. 十年感悟之 python之路
  5. 前端之:传统的DOM是如何渲染的?
  6. stm32 ADXL345传感器
  7. Feign报错&#39;xx.FeignClientSpecification&#39;, defined in null, could not be registered.
  8. linux跳板机开发之trap信号机应用
  9. Powershell-获取Hyper-V复制状态
  10. Android笔记(十七) Android中的Service