在并发环境下,解决共享资源冲突问题时,可以考虑使用锁机制。
1.对象的锁
所有对象都自动含有单一的锁。
JVM负责跟踪对象被加锁的次数。如果一个对象被解锁,其计数变为0。在任务(线程)第一次给对象加锁的时候,计数变为1。每当这个相同的任务(线程)在此对象上获得锁时,计数会递增。
只有首先获得锁的任务(线程)才能继续获取该对象上的多个锁。
每当任务离开一个synchronized方法,计数递减,当计数为0的时候,锁被完全释放,此时别的任务就可以使用此资源。
2.synchronized同步块
2.1同步到单一对象锁
当使用同步块时,如果方法下的同步块都同步到一个对象上的锁,则所有的任务(线程)只能互斥的进入这些同步块。
Resource1.java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中,虽然这些同步块处在不同的方法中,但由于是同步到同一个对象(当前对象 synchronized (this)),所以对它们的方法依然是互斥的。
Resource1.java
package com.zj.lock;<br />import java.util.concurrent.TimeUnit;<br /><br />public class Resource1 {<br /> public void f() {<br /> // other operations should not be locked...<br /> System.out.println(Thread.currentThread().getName()<br /> + ":not synchronized in f()");<br /> synchronized (this) {<br /> for (int i = 0; i < 5; i++) {<br /> System.out.println(Thread.currentThread().getName()<br /> + ":synchronized in f()");<br /> try {<br /> TimeUnit.SECONDS.sleep(3);<br /> } catch (InterruptedException e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> }<br /> }<br /><br /> public void g() {<br /> // other operations should not be locked...<br /> System.out.println(Thread.currentThread().getName()<br /> + ":not synchronized in g()");<br /> synchronized (this) {<br /> for (int i = 0; i < 5; i++) {<br /> System.out.println(Thread.currentThread().getName()<br /> + ":synchronized in g()");<br /> try {<br /> TimeUnit.SECONDS.sleep(3);<br /> } catch (InterruptedException e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> }<br /> }<br /><br /> public void h() {<br /> // other operations should not be locked...<br /> System.out.println(Thread.currentThread().getName()<br /> + ":not synchronized in h()");<br /> synchronized (this) {<br /> for (int i = 0; i < 5; i++) {<br /> System.out.println(Thread.currentThread().getName()<br /> + ":synchronized in h()");<br /> try {<br /> TimeUnit.SECONDS.sleep(3);<br /> } catch (InterruptedException e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> }<br /> }<br /><br /> public static void main(String[] args) {<br /> final Resource1 rs = new Resource1();<br /><br /> new Thread() {<br /> public void run() {<br /> rs.f();<br /> }<br /> }.start();<br /><br /> new Thread() {<br /> public void run() {<br /> rs.g();<br /> }<br /> }.start();<br /><br /> rs.h();<br /> }<br />}
结果:
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;impor 请让我们从容面对这离别之后的离别。