- 浏览: 29868 次
- 性别:
- 来自: 宁波
文章分类
最新评论
-
zpd00001:
大道理,刚毕业的菜鸟们是不懂的- -!!
毕业后五年之内将决定你的一生 人生与励志 -
li_47195:
看完后我沉默很久..所谓“优秀生”?..一篇转载,献给所有和我一样迷茫没有目标的人
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://zhangjunhd.blog.51cto.com/113473/70300
在并发环境下,解决共享资源冲突问题时,可以考虑使用锁机制。
1.对象的锁
所有对象都自动含有单一的锁。
JVM负责跟踪对象被加锁的次数。如果一个对象被解锁,其计数变为0。在任务(线程)第一次给对象加锁的时候,计数变为1。每当这个相同的任务(线程)在此对象上获得锁时,计数会递增。
只有首先获得锁的任务(线程)才能继续获取该对象上的多个锁。
每当任务离开一个synchronized方法,计数递减,当计数为0的时候,锁被完全释放,此时别的任务就可以使用此资源。
2.synchronized同步块
2.1同步到单一对象锁
当使用同步块时,如果方法下的同步块都同步到一个对象上的锁,则所有的任务(线程)只能互斥的进入这些同步块。
Resource1.java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中,虽然这些同步块处在不同的方法中,但由于是同步到同一个对象(当前对象 synchronized (this)),所以对它们的方法依然是互斥的。
Resource1.java
结果:
Thread-0:not synchronized in f()
Thread-0:synchronized in f()
main:not synchronized in h()
Thread-1:not synchronized in g()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
2.2 同步到多个对象锁
Resource1.java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中,这些同步块处在不同的方法中,并且是同步到三个不同的对象(synchronized (this),synchronized (syncObject1),synchronized (syncObject2)),所以对它们的方法中的临界资源访问是独立的。
Resource2.java
结果:
Thread-0:not synchronized in f()
Thread-0:synchronized in f()
main:not synchronized in h()
main:synchronized in h()
Thread-1:not synchronized in g()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
3.Lock对象锁
除了使用synchronized外,还可以使用Lock对象来创建临界区。Resource3.java的演示效果同Resource1.java;Resource4.java的演示效果同Resource2.java。
Resource3.java
结果:
Thread-0:not synchronized in f()
Thread-0:synchronized in f()
main:not synchronized in h()
Thread-1:not synchronized in g()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Resource4.java
结果:
Thread-0:not synchronized in f()
Thread-0:synchronized in f()
main:not synchronized in h()
main:synchronized in h()
Thread-1:not synchronized in g()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/70300
在并发环境下,解决共享资源冲突问题时,可以考虑使用锁机制。
1.对象的锁
所有对象都自动含有单一的锁。
JVM负责跟踪对象被加锁的次数。如果一个对象被解锁,其计数变为0。在任务(线程)第一次给对象加锁的时候,计数变为1。每当这个相同的任务(线程)在此对象上获得锁时,计数会递增。
只有首先获得锁的任务(线程)才能继续获取该对象上的多个锁。
每当任务离开一个synchronized方法,计数递减,当计数为0的时候,锁被完全释放,此时别的任务就可以使用此资源。
2.synchronized同步块
2.1同步到单一对象锁
当使用同步块时,如果方法下的同步块都同步到一个对象上的锁,则所有的任务(线程)只能互斥的进入这些同步块。
Resource1.java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中,虽然这些同步块处在不同的方法中,但由于是同步到同一个对象(当前对象 synchronized (this)),所以对它们的方法依然是互斥的。
Resource1.java
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(); } }
结果:
Thread-0:not synchronized in f()
Thread-0:synchronized in f()
main:not synchronized in h()
Thread-1:not synchronized in g()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
2.2 同步到多个对象锁
Resource1.java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中,这些同步块处在不同的方法中,并且是同步到三个不同的对象(synchronized (this),synchronized (syncObject1),synchronized (syncObject2)),所以对它们的方法中的临界资源访问是独立的。
Resource2.java
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(); } }
结果:
Thread-0:not synchronized in f()
Thread-0:synchronized in f()
main:not synchronized in h()
main:synchronized in h()
Thread-1:not synchronized in g()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
3.Lock对象锁
除了使用synchronized外,还可以使用Lock对象来创建临界区。Resource3.java的演示效果同Resource1.java;Resource4.java的演示效果同Resource2.java。
Resource3.java
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(); } }
结果:
Thread-0:not synchronized in f()
Thread-0:synchronized in f()
main:not synchronized in h()
Thread-1:not synchronized in g()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
Thread-0:synchronized in f()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Thread-1:synchronized in g()
Resource4.java
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(); } }
结果:
Thread-0:not synchronized in f()
Thread-0:synchronized in f()
main:not synchronized in h()
main:synchronized in h()
Thread-1:not synchronized in g()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
Thread-0:synchronized in f()
main:synchronized in h()
Thread-1:synchronized in g()
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/70300
发表评论
-
【转】PC安卓模拟器PANIC: Could not open:C:\Documents and Settings\Administrator\.android
2013-03-13 23:43 746在初次运行Android程序的 ... -
再谈重入锁--ReentrantLock
2012-12-26 20:50 362重入锁(ReentrantLock)是一种递归无阻塞的同步机制 ... -
Spring 设置支态定时任务
2012-11-27 12:27 615什么是动态定时任务:是由客户制定生成的,服务端只知道 ... -
Java TCP/IP Socket 编程 笔记(四)—发送和接收数据
2012-10-29 20:17 18621.TCP/IP协议要求信息必须在块(chunk)中发送和接收 ... -
Java TCP/IP Socket 编程 笔记(三)—UDP的例子
2012-10-29 19:53 8781.UDP套接字与TCP套接字 ... -
Java TCP/IP Socket 编程 笔记(二)—TCP的例子
2012-10-29 19:49 7291.InetAddress类和SocketAddress用于 ... -
Java TCP/IP Socket 编程 笔记(一)—基本概念
2012-10-29 19:42 797一些概念: 通信信道(communication c ... -
java并发编程不得不知道的几件事(转载)
2012-10-29 19:31 617多线程编 ... -
Web.XML 配置详解
2012-09-07 11:56 552每一个站的WEB-INF下都有一个web.xml的设定文件, ... -
java 多线程编程需要注意的23条
2012-08-13 15:56 5501.多线程中有主内存和 ... -
感受Java中的多线程设计
2012-08-13 15:49 628我就不说最初那个单核CPU时代了,我们从多进程编程开始讲。 ... -
排序算法(JAVA)(二)归并排序,堆排序,桶式排序,基数排序
2012-07-11 09:05 515六 归并排序 算法思想是每次把待排序列分成两部分,分别对这两部 ... -
排序算法(JAVA)(一)插入排序,冒泡排序,选择排序,Shell,快速排序
2012-07-11 08:59 697为了便于管理,先引入个基础类: package algor ... -
理解ThreadLocal
2012-04-28 16:05 0ThreadLocal是什么 早在JDK ... -
Java反射机制
2011-12-31 13:16 637http://www.cnblogs.com/Quincy/ ... -
Java5.0多线程编程
2011-11-22 16:00 680[size=large] Lock接口 ReentrantL ... -
Java Map遍历的方法
2011-11-09 19:41 808第一种:利用entryset遍历 Map map = ne ... -
Java常见异常汇总
2011-11-09 19:27 633转自于: http://www.javaask.com/jav ... -
java io/流
2011-11-09 19:20 453[转]JAVA IO流 http://www.blogjava ... -
Java线程:创建与启动
2011-10-31 13:50 567一、定义线程 1、扩展 ...
相关推荐
同样是锁,先说说synchronized和lock的...2.synchronized可以锁住代码块,对象和类,但是线程从开始获取锁之后开发者不能进行控制和了解;lock则用起来非常灵活,提供了许多api可以让开发者去控制加锁和释放锁等等。
而Lock接口则是一个更底层的同步机制,它提供了更丰富的功能,但需要显式地获取和释放锁,通常通过实现类如ReentrantLock来使用。 其次,从功能特性上来看,synchronized具有可重入性,即同一个线程可以多次获取同...
Java 为程序加锁的方式主要有两种:synchronized 与 Lock。 1. synchronized 可以修饰的作用域如下: - 非静态方法(加的锁为对象锁); - 静态方法(加的锁为类锁); - 代码块(对象锁与类锁均可); 2. Lock ...
此时,如果其他线程试图获取该对象的锁,JVM会将对象头中的Mark Word(指向对象头的指针)设置为偏向锁标识。这样,只有当前线程才能访问该对象,其他线程需要等待偏向锁失效后才能继续执行。 当一个线程释放偏向锁时...
三、线程同步和互斥锁:解释了线程同步的概念,以及互斥锁的作用,包括使用synchronized关键字和Lock接口实现线程同步的方式。 四、反射机制:解释了Java中的反射机制,包括在运行时动态获取类的信息,操作类的属性...
此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数据慢,Vector由于使用了synchronized方法(线程安全),...
7.6.1 超车方法:使用类实例做参数 172 7.6.2 调用这个方法 173 7.6.3 发生了什么 174 7.7 加餐:局部变量和实例变量 175 7.7.1 什么是局部变量(Local Variable) 175 7.7.2 什么是实例变量(Instance ...
此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数据慢,Vector由于使用了synchronized方法(线程安全),...
它会解除由线程获取的所有锁定,而且如果对象处于一种不连贯状态,那么其他线程能在那种状态下检查和修改它们。结果很难检查出真正的问题所在。suspend()方法容易发生死锁。调用suspend()的时候,目标线程会停下来,...
7.6.1 超车方法:使用类实例做参数 172 7.6.2 调用这个方法 173 7.6.3 发生了什么 174 7.7 加餐:局部变量和实例变量 175 7.7.1 什么是局部变量(Local Variable) 175 7.7.2 什么是实例变量(Instance ...
简述synchronized和java.util.concurrent.locks.Lock的异同 ? 34.EJB规范规定EJB中禁止的操作有哪些? 35.java除了8种基本类型外,在虚拟机里还有哪一种,有什么作用? 36.除了使用new关键字创建对象意外,试列举...
在类中利用synchronized修饰的方法或者this代码块,均使用的是类的实例锁或者类的锁。这些锁都称为内置锁。 可以利用显式锁进行协调对象的访问。即ReentrantLock。这是一种可以提供无条件,可轮询,定时以及...
54.synchronized 和 Lock 有什么区别? 55.synchronized 和 ReentrantLock 区别是什么? 56.说一下 atomic 的原理? 四、反射 57.什么是反射? 58.什么是 java 序列化?什么情况下需要序列化? 59.动态代理是什么?...
1.3 线程信息的获取和设置 5 1.4 线程的中断 9 1.5 线程中断的控制 11 1.6 线程的休眠和恢复 15 1.7 等待线程的终止 17 1.8 守护线程的创建和运行 20 1.9 线程中不可控异常的处理 24 1.10 ...
3.4.2 示例:使用Volatile类型来发布不可变对象40 3.5 安全发布41 3.5.1 不正确的发布:正确的对象被破坏42 3.5.2 不可变对象与初始化安全性42 3.5.3 安全发布的常用模式43 3.5.4 事实不可变对象44 3.5.5 可变...
14. 简述synchronized和java.util.concurrent.locks.Lock的异同 ? 11 15. 当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 11 16. abstract class和interface有什么区别? 12...
77、简述synchronized和java.util.concurrent.locks.Lock的异同 ? 18 78、abstract class Name { private String name; public abstract boolean isStupidName(String name) {}}这有何错误? 18 79、public class ...
所以很少存在简单重复的工作,加上Java 代码的精炼性和面向对象纯洁性(设计模式是 java 的灵魂),编程工作将变成一个让你时刻 体验创造快感的激动人心的过程. 为能和大家能共同探讨"设计模式",我将自己在学习中的心得...
在 Python 中没有类似 Java 中使用的 synchronized 关键字来同步方法,因此在 Python 中要实现同步方法,通常我们是使用 threading.Lock() 来实现。在进入函数的地方获取锁,出函数的时候释放锁,这样实现代码看起好...