Java:使用synchronized和Lock对象获取对象锁
package com.zj.lock;
import java.util.concurrent.TimeUnit;
public class Resource1 {
public void f() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in f()");
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in f()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void g() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in g()");
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in g()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void h() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in h()");
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in h()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
final Resource1 rs = new Resource1();
new Thread() {
public void run() {
rs.f();
}
}.start();
new Thread() {
public void run() {
rs.g();
}
}.start();
rs.h();
}
}
|
package com.zj.lock;
import java.util.concurrent.TimeUnit;
public class Resource2 {
private Object syncObject1 = new Object();
private Object syncObject2 = new Object();
public void f() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in f()");
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in f()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void g() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in g()");
synchronized (syncObject1) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in g()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void h() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in h()");
synchronized (syncObject2) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in h()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
final Resource2 rs = new Resource2();
new Thread() {
public void run() {
rs.f();
}
}.start();
new Thread() {
public void run() {
rs.g();
}
}.start();
rs.h();
}
}
|
package com.zj.lock;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Resource3 {
private Lock lock = new ReentrantLock();
public void f() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in f()");
lock.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in f()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock.unlock();
}
}
public void g() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in g()");
lock.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in g()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock.unlock();
}
}
public void h() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in h()");
lock.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in h()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
final Resource3 rs = new Resource3();
new Thread() {
public void run() {
rs.f();
}
}.start();
new Thread() {
public void run() {
rs.g();
}
}.start();
rs.h();
}
}
|
package com.zj.lock;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Resource4 {
private Lock lock1 = new ReentrantLock();
private Lock lock2 = new ReentrantLock();
private Lock lock3 = new ReentrantLock();
public void f() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in f()");
lock1.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in f()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock1.unlock();
}
}
public void g() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in g()");
lock2.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in g()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock2.unlock();
}
}
public void h() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in h()");
lock3.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in h()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock3.unlock();
}
}
public static void main(String[] args) {
final Resource4 rs = new Resource4();
new Thread() {
public void run() {
rs.f();
}
}.start();
new Thread() {
public void run() {
rs.g();
}
}.start();
rs.h();
}
}
|
Java:使用synchronized和Lock对象获取对象锁的更多相关文章
- Java中synchronized和Lock的区别
synchronized和Lock的区别synchronize锁对象可以是任意对象,由于监视器方法必须要拥有锁对象那么任意对象都可以调用的方法所以将其抽取到Object类中去定义监视器方法这样锁对象和 ...
- java中synchronized与Lock的异同
本文转载自java中synchronized与Lock的异同 前言 synchronized和Lock通过互斥保障原子性,能够保护共享数据以实现线程安全,其作用包括保障原子性.可见性.有序性 常见问题 ...
- JAVA中synchronized和lock详解
目前在Java中存在两种锁机制:synchronized和Lock,Lock接口及其实现类是JDK5增加的内容,其作者是大名鼎鼎的并发专家Doug Lea.本文并不比较synchronize ...
- 【Java】synchronized与lock的区别
从Java 5之后,在java.util.concurrent.locks包下提供了另外一种方式来实现同步访问,那就是Lock. 也许有朋友会问,既然都可以通过synchronized来实现同步访问了 ...
- java多线程:synchronized和lock比较浅析
转载:http://www.toutiao.com/a6392135944652587266/?tt_from=weixin&utm_campaign=client_share&app ...
- java 多线程 synchronized与lock的通信机制等问题,结合相应实例说明
1. 利用多线程实现如下需求: 写两个线程,一个线程打印1~52,另一个线程打印A~Z,打印顺序是12A34B...5152Z: 2. 使用synchronized 实现 public class T ...
- java多线程 synchronized 与lock锁 实现线程安全
如果有多个线程在同时运行,而这些线程可能会同时运行这段代码.程序每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就是线程安全的. 通过卖火车票的例子 火车站要卖票,我们模 ...
- Java中Synchronized和Lock的使用
Lock的锁定是通过代码实现的,而 synchronized 是在 JVM 层面上实现的 synchronized在锁定时如果方法块抛出异常,JVM 会自动将锁释放掉,不会因为出了异常没有释放锁造成线 ...
- java面试-synchronized与lock有什么区别?
1.原始构成: synchronized是关键字,属于JVM层面,底层是由一对monitorenter和monitorexit指令实现的. ReentrantLock是一个具体类,是API层面的锁. ...
随机推荐
- SQL每个月份的发生额都比101科目多的科目
请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目.请注意:TestDB中有很多科目,都有1-12月份的发生额. ...
- mac outlook无法发送邮件
工具-帐户 第一步把SSL钩挑上 第二步 下面的更多选项,验证选择“使用接收服务器信息” 搞定了!记得个赞!
- Python学习笔记 (4) :迭代器、生成器、装饰器、递归、正则表达式等
迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优点是 ...
- 设置Firefox禁用js缓存
让firefox禁用缓存 让Firefox不再使用缓存网站开发时经常会有这样的疑问:为什么修改了代码,刷新了页面还是没有看到改动呢? 其实,可能只是你的Firefox并没有去下载你更新了的文件. 这时 ...
- Strata 2014 上的 AzureCAT 粉笔会谈
本周,AzureCAT 团队非常高兴在 Strata 会议上首次集体亮相.对于那些对 AzureCAT 团队不太熟悉的人来说,我们是 Microsoft 云与企业部门一个核心的国际性团队,由大约 ...
- BestCoder Round #61 (div.2)
Numbers Accepts: 571 Submissions: 1212 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/26 ...
- leetcode_question_67 Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- Html.raw(转帖)
Razor 在JS中嵌入后台变量 HTML 中定义全局变量 @{int CurrentUserId =ViewBag.CurrentUserId;} JS中取值方式var CurrentUserId ...
- JavaScript constructor prototyoe
想加深一下自己对construtcor prototype的印象所以写了这一篇文章 对象的constructor 就是Object 除了通过构造函数创建的对象意外 他的constructor 都是 都 ...
- 专门针对初学者的Node.js教程
转载原文:http://www.csdn.net/article/2013-08-28/2816731-absolute-beginners-guide-to-nodejs Node.js的教程并不缺 ...