- 浏览: 416526 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
burningblood:
最近也遇到了这个细节问题。我用的是4,里面没有 get.rel ...
httpclient的并发连接问题 -
greatwqs:
使用HttpURLConnection注意设置超时 -
qinweilh:
...
tomcat报错:standardServer.await: create[8005]: -
jayyunfei:
还是不很明白
JPA entityManager的管理 -
a418040445:
...
Calendar
JAVA 的内存模型是对每一个进程有一个主内存, 每个线程有自己的内存, 他们从主内存中取数据, 然后计算, 再存入主内存中。
并发问题如下:如果多个线程同事操作同一数据, A线程从主内存中取的I的值为1, 然后进行加1操作, 这时B线程也取I的值, 进行加2操作, 然后A存入2到主内存中, B也存入, 这样就覆盖了A的值(同数据库中的并发问题一样)。
解决办法是用synchronized , 如用synchronized(I)。被synchronized 修饰的方法(块)把以下三步操作当成一个原子操作:取数据, 操作数据, 存数据。 我们知道原子操作是不可以被打断的, 所以其保证了数据一致性, 这样同一时间只有一个线程再执行, 对性能有一定的影响。这也是synchronized 的第二个作用:保证同一时间只有一个线程在运行。 当实现SOCKET连接的时候经常用到.
JAVA中规定对非FLOAT, LONG的原始类型的取和存操作为原子操作。 其实就是对一个字(32位)的取,存位原始操作, 因为FLOAT, LONG为两个字节的长度, 所以其取, 存为非原子操作。 如果想把他们也变为原子操作, 可以用VOLATILE关键字来修饰.
Synchronization is built around an internal entity known as the intrinsic lock or monitor lock . (The API specification often refers to this entity simply as a "monitor.") Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility.Every object has an intrinsic lock associated with it. By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them. A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock. As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock.
When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquistion of the same lock.
Locks In Synchronized Methods
When a thread invokes a synchronized method, it automatically acquires the intrinsic lock for that method's object and releases it when the method returns. The lock release occurs even if the return was caused by an uncaught exception.You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the
Class
object associated with the class. Thus access to class's static fields is controlled by a lock that's distinct from the lock for any instance of the class.Synchronized Statements
Another way to create synchronized code is with synchronized statements . Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock:In this example, thepublic void addName(String name) { synchronized(this) { lastName = name; nameCount++; } nameList.add(name); }addName
method needs to synchronize changes tolastName
andnameCount
, but also needs to avoid synchronizing invocations of other objects' methods. (Invoking other objects' methods from synchronized code can create problems that are described in the section on Liveness .) Without synchronized statements, there would have to be a separate, unsynchronized method for the sole purpose of invokingnameList.add
.Synchronized statements are also useful for improving concurrency with fine-grained synchronization. Suppose, for example, class
MsLunch
has two instance fields,c1
andc2
, that are never used together. All updates of these fields must be synchronized, but there's no reason to prevent an update of c1 from being interleaved with an update of c2 — and doing so reduces concurrency by creating unnecessary blocking. Instead of using synchronized methods or otherwise using the lock associated withthis
, we create two objects solely to provide locks.Use this idiom with extreme care. You must be absolutely sure that it really is safe to interleave access of the affected fields.public class MsLunch { private long c1 = 0; private long c2 = 0; private Object lock1 = new Object(); private Object lock2 = new Object(); public void inc1() { synchronized(lock1) { c1++; } } public void inc2() { synchronized(lock2) { c2++; } } }Reentrant Synchronization
Recall that a thread cannot acquire a lock owned by another thread. But a thread can acquire a lock that it already owns. Allowing a thread to acquire the same lock more than once enables reentrant synchronization . This describes a situation where synchronized code, directly or indirectly, invokes a method that also contains synchronized code, and both sets of code use the same lock. Without reentrant synchronization, synchronized code would have to take many additional precautions to avoid having a thread cause itself to block.
发表评论
-
糟糕透顶的axis2
2015-02-06 15:27 822我记得很早以前在网上看过文章,大致讲axis2的质量如何 ... -
使用java连接https的问题
2012-03-19 15:41 806在使用hudson的过程中,我们需要发送邮件的功能。但是公司的 ... -
SSL exception: "No subject alternative names matching IP address ..." & "No name
2012-02-08 09:35 10379When you want to establish an S ... -
ResourceBundle加载文件的顺序
2011-12-24 15:01 1314If a ResourceBundle class for ... -
异常处理框架
2011-09-07 14:57 782The Nature of Exceptions Bro ... -
httpclient的并发连接问题
2011-05-24 16:14 6647昨天的搜索系统又出状况了,几个库同时重建索引变得死慢。经 ... -
java connect https
2011-05-04 15:37 945When I use java to connect HTTP ... -
jvm的高性能
2011-04-25 13:48 773jdk将源代码编译成字节码之后,由JVM在运行期对其进行解释执 ... -
java annotation
2011-03-10 14:58 847JDK内置的annotaion 1. @Target ... -
java字节码的操纵
2011-03-09 16:35 1481http://www.infoq.com/cn/article ... -
java 范型
2011-03-09 15:15 700Java泛型(generics)是JDK 5中引入的一个新特性 ... -
正确使用 Volatile 变量
2011-03-04 09:34 741Java 语言中的 volatile 变 ... -
java中Thread与Runnable的区别
2011-02-25 20:42 1692在java中可有两种方式实现多线程,一种是继承Thread类, ... -
copy-on-write
2010-12-08 10:29 889Copy-on-write (sometimes refe ... -
Map 四种同步方式的性能比较
2010-11-25 11:50 926如果需要使 Map 线程安全,大致有这么四种方法: 1、 ... -
Java中HashMap,LinkedHashMap,TreeMap的区别
2010-11-25 11:49 1708java为数据结构中的映射 ... -
java的内存泄漏
2010-11-25 10:52 10041 引言 Java的一个重要优点就是通过垃圾收集器 ... -
解析Java对象的equals()和hashCode()的使用
2010-11-25 10:49 1049前言 在Java语言中,equals()和hash ... -
java map
2010-11-25 10:40 10711. Map key: 同一个key必须hashcode相同。 ... -
Calendar
2010-09-17 15:44 1405对于时间的运算,应当使用Calendar: Calendar ...
相关推荐
Synchronized原理-思维导图
由于对象头的信息是与对象自身定义的数据没有关系的额外存储成本,因此考虑到JVM的空间效率,Mark Word 被设计成为一个非固定的数据结构,以便存储更多有效的数据,它会根据对象本身的状态复用自己的存储空间,如32...
synchronized关键字”锁”的实现本质 synchronized关键字实现的锁是依赖于JVM的,底层调用的是操作系统的指令集实现。 Lock接口实现的锁不一样,例如ReentrantLock锁是基于JDK实现的,有Java原生代码来实现的。 ...
java synchronized关键字原理、自定义一把锁来实现同步等
synchronized底层原理解析_1
synchronized底层原理解析_2
synchronized底层原理解析_3
synchronized底层原理解析_4
synchronized底层原理解析_5
synchronized底层原理解析_6
synchronized底层原理解析_7
synchronized底层原理解析_8
主要介绍了Java中synchronized实现原理详解,涉及synchronized实现同步的基础,Java对象头,Monitor,Mark Word,锁优化,自旋锁等相关内容,具有一定借鉴价值,需要的朋友可以参考下。
原理 synchronized关键字是通过字节码指令来实现的 synchronized关键字编译后会在同步块前后形成monitorenter和monitorexit两个字节码指令 执行monitorenter指令时需要先获得对象的锁(每个对象有一个监视器锁...
【Java并发编程】Synchronized关键字实现原理.doc
在 Java 中,关键字 synchronized可以保证在同一个时刻,只有一个线程可以执行某个方法或者某个代码块(主要是对方法或者代码块中存在共享数据的操作),同时我们还应该注意到synchronized另外一个重要的作用,...
并发编程原理学习:synchronized关键字.doc
该内容是在暮课网学习的课程比较,讲解synchronized的原理和用法,以及常见的面试题。上传网上以供自己以后复习。
synchronized.pdf
主要介绍了PHP pthreads v3下同步处理synchronized用法,结合实例形式分析了PHP pthreads v3下同步处理synchronized原理、使用方法及相关操作注意事项,需要的朋友可以参考下