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层面的锁. ...
随机推荐
- sort 与 qsort
很长一段时间搞不明白 sort 和 qsort 的区别,平时在写程序时习惯了使用 sort ,因为它用起来比 qsort 要简单的多 , 这里详细介绍一下 sort 与 qsort : 给出一个数组 ...
- BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )
有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...
- Java学习之内部类
示例1: package com.swust.面向对象; class Person1{ private String username="zhangsan"; public Per ...
- PHP学习笔记5-类的继承/方法重写
更改Man.php文件名为People.php,加入代码: public function hi(){ return $this->_name.' say hi'; } 新建文件Man.php: ...
- 射频识别技术漫谈(9)——动物标签HDX
半双工(HDX,Half Duplex)技术是ISO11784/11785中规定的另一种标签与读写器之间的通讯方式.读写器先打开射频场对标签充电以激活标签,然后关闭磁场,标签在读写器磁场关闭的情况下向 ...
- cocos2d-x中的尺寸之三
通过上面两个文章的分析,我们在这个博文里做个总结: CCEGLView::getFrameSize()返回的是窗口相对于屏幕像素的尺寸,这个尺寸,只要窗口没变化,值就不会变化 CCDirector:: ...
- Win32 GDI基础(笔记)
1.GDI名字的意义 GDI Graphic Device Interface,我说不清和GUI有什么区别.可能一种针对设备,一种针对用户而言吧,反正以后都说GDI,也就是Windows的图形编程. ...
- JAVA GUI学习 - JProgressBar进度条组件摘录
public class JProgressBarTest extends JFrame{ public JProgressBarTest() { super(); setTitle("表格 ...
- [Leetcode]-containsNearbyDuplicate
//题目: //给定一个整数数组与一个整数k,当且存在两个不同的下标i和j满足nums[i] = nums[j]而且| i - j | <= k时返回true.否则返回false. #inclu ...
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
In this tutorial, I would like to help out Magento developers and clients with how to programmatical ...