Java并发编程示例代码-----ReentrantLock
2024-08-28 08:29:41
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);
}
}
最新文章
- jquery选择器使用说明
- Java、Hibernate(JPA)注解大全
- 20160314 Servlet 入门
- bing---iis how to process http request
- 最最常用的 100 个 Java 类(转)
- Linq to Sqlite连接
- POJ 1021 2D-Nim
- 90. Subsets II(中等,编写代码有难度)
- ZooKeeper之(四)配置与命令
- Scala变量| 流程控制
- 一对多关联模型,BELONGS_TO
- python操作pymysql数据库
- angular.copy()
- Golang标准库——io-结构
- Ansible系列(四):playbook应用和roles自动化批量安装示例
- JavaScript简易教程
- LR-事务
- netstat命令初探
- xdebug的安装测试
- 第一次软件工程作业补充plus
热门文章
- C#压缩和解压文件
- Python windows环境 搭建问题
- kvm第三章--虚拟化存储管理
- 十年感悟之 python之路
- 前端之:传统的DOM是如何渲染的?
- stm32 ADXL345传感器
- Feign报错&#39;xx.FeignClientSpecification&#39;, defined in null, could not be registered.
- linux跳板机开发之trap信号机应用
- Powershell-获取Hyper-V复制状态
- Android笔记(十七) Android中的Service