- 浏览: 426563 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
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 859我记得很早以前在网上看过文章,大致讲axis2的质量如何 ... -
使用java连接https的问题
2012-03-19 15:41 856在使用hudson的过程中,我们需要发送邮件的功能。但是公司的 ... -
SSL exception: "No subject alternative names matching IP address ..." & "No name
2012-02-08 09:35 10748When you want to establish an S ... -
ResourceBundle加载文件的顺序
2011-12-24 15:01 1365If a ResourceBundle class for ... -
异常处理框架
2011-09-07 14:57 824The Nature of Exceptions Bro ... -
httpclient的并发连接问题
2011-05-24 16:14 6686昨天的搜索系统又出状况了,几个库同时重建索引变得死慢。经 ... -
java connect https
2011-05-04 15:37 1019When I use java to connect HTTP ... -
jvm的高性能
2011-04-25 13:48 807jdk将源代码编译成字节码之后,由JVM在运行期对其进行解释执 ... -
java annotation
2011-03-10 14:58 905JDK内置的annotaion 1. @Target ... -
java字节码的操纵
2011-03-09 16:35 1532http://www.infoq.com/cn/article ... -
java 范型
2011-03-09 15:15 757Java泛型(generics)是JDK 5中引入的一个新特性 ... -
正确使用 Volatile 变量
2011-03-04 09:34 789Java 语言中的 volatile 变 ... -
java中Thread与Runnable的区别
2011-02-25 20:42 1745在java中可有两种方式实现多线程,一种是继承Thread类, ... -
copy-on-write
2010-12-08 10:29 932Copy-on-write (sometimes refe ... -
Map 四种同步方式的性能比较
2010-11-25 11:50 975如果需要使 Map 线程安全,大致有这么四种方法: 1、 ... -
Java中HashMap,LinkedHashMap,TreeMap的区别
2010-11-25 11:49 1749java为数据结构中的映射 ... -
java的内存泄漏
2010-11-25 10:52 10501 引言 Java的一个重要优点就是通过垃圾收集器 ... -
解析Java对象的equals()和hashCode()的使用
2010-11-25 10:49 1091前言 在Java语言中,equals()和hash ... -
java map
2010-11-25 10:40 11571. Map key: 同一个key必须hashcode相同。 ... -
Calendar
2010-09-17 15:44 1458对于时间的运算,应当使用Calendar: Calendar ...
相关推荐
Synchronized原理-思维导图
Java中的`synchronized`关键字是实现线程安全的关键机制,它基于Java对象头的Mark Word进行锁的状态管理。Mark Word是一个动态变化的数据结构,用于存储对象...理解这些锁的工作原理对于编写高性能的并发代码至关重要。
Java 并发编程 Synchronized 关键字实现原理 Synchronized 关键字是 Java 并发编程中最基本的同步机制,它可以保证线程安全,包括原子性、可见性和有序性。Synchronized 关键字可以修饰方法或代码块,使得在同一...
《深入剖析synchronized锁原理——从Java对象头的角度》 synchronized关键字在Java中扮演着重要的角色,它是Java实现同步的基础,确保了多线程环境下的数据一致性。不同于基于JDK实现的Lock接口(如ReentrantLock)...
**synchronized原理:** 1. **对象锁**:每个Java对象都可以作为一个锁,当调用对象的`synchronized`方法或在代码块中使用`synchronized`关键字时,会获取到这个对象的锁。如果多个线程尝试访问同一个对象的同步...
synchronized底层原理解析_1
synchronized底层原理解析_2
synchronized底层原理解析_3
synchronized底层原理解析_4
synchronized底层原理解析_5
synchronized底层原理解析_6
synchronized底层原理解析_7
synchronized底层原理解析_8
### Java synchronized 关键字原理与自定义锁实现详解 #### 一、Java synchronized 关键字原理 `synchronized` 是 Java 中的关键字之一,用于实现线程间的同步控制,确保共享资源的安全访问。它主要应用于以下两种...
**问题一:Synchronized原理是什么?** `synchronized`是JVM层面实现的一种同步机制,它通过在字节码指令中使用`monitorenter`和`monitorexit`来控制线程对对象的锁定和解锁。当线程执行到`monitorenter`时,它尝试...
Java 面试问题是 Java 开发者的必备知识之一,本文将对 Java 面试问题进行详细的解释和分析,涵盖进程和线程的区别、synchronized 原理、锁的优化机制、对象头的组成等知识点。 一、进程和线程的区别 进程是程序的...
1. **synchronized原理**: `synchronized`是Java中用于实现线程同步的关键字。它通过在对象头设置标记来实现锁的获取与释放。在字节码层面,被`synchronized`修饰的代码块会在编译前后分别插入`monitorenter`和`...
Java中的`synchronized`关键字是实现线程同步的关键,它的实现原理涉及到多个方面,包括Java对象头、Monitor(监视器锁)以及一系列的锁优化策略。本文将深入探讨这些概念。 首先,`synchronized`用于保证多线程...
1. **synchronized原理** synchronized基于Java的内置锁(也称为对象锁或 monitors)机制。当一个线程进入synchronized代码块或方法时,它会获取与该代码块或方法关联的对象锁。其他尝试进入相同代码块或方法的线程...
1. Synchronized原理:Synchronized是通过在对象头设置标记,来实现获取锁和释放锁的过程。具体来说,当Synchronized修饰的代码块执行时,JVM生成monitorenter和monitorexit两个字节码指令。monitorenter指令用于...