由于同一进程的多个线程共享同一片存储空间,在带来方便的同时,也带来了访问冲突这个严重的问题。Java语言提供了专门机制以解决这种冲突,有效避免了同一个数据对象被多个线程同时访问。 需要明确的几个问题:
每个类实例对应一把锁,每个 synchronized 方法都必须获得调用该方法的类实例的锁方能执行,否则所属线程阻塞,方法一旦执行,就独占该锁,直到从该方法返回时才将锁释放,此后被阻塞的线程方能获得该锁,重新进入可执行状态。这种机制确保了同一时刻对于每一个类实例,其所有声明为 synchronized 的成员函数中至多只有一个处于可执行状态(因为至多只有一个能够获得该类实例对应的锁),从而有效避免了类成员变量的访问冲突(只要所有可能访问类成员变量的方法均被声明为 synchronized)。 在 Java 中,不光是类实例,每一个类也对应一把锁,这样我们也可将类的静态成员函数声明为 synchronized ,以控制其对类的静态成员变量的访问。
同步方法,这时synchronized锁定的是哪个对象呢?它锁定的是调用这个同步方法对象。也就是说,当一个对象 P1在不同的线程中执行这个同步方法时,它们之间会形成互斥,达到同步的效果。但是这个对象所属的Class所产生的另一对象P2却可以任意调用这个被加 了synchronized关键字的方法.同步方法实质是将synchronized作用于object reference。――那个拿到了P1对象锁的线程,才可以调用P1的同步方法,而对P2而言,P1这个锁与它毫不相干,程序也可能在这种情形下摆脱同步机制的控制,造成数据混乱:( ;若将一个大的方法声明为synchronized 将会大大影响效率,典型地,若将线程类的方法 run() 声明为 synchronized ,由于在线程的整个生命期内它一直在运行,因此将导致它对本类任何 synchronized 方法的调用都永远不会成功。当然我们可以通过将访问类成员变量的代码放到专门的方法中,将其声明为 synchronized ,并在主方法中调用来解决这一问题,但是 Java 为我们提供了更好的解决办法,那就是 synchronized 块。
除了方法前用synchronized关键字,synchronized关键字还可以用于方法中的某个区块中,表示只对这个区块的资源实行互斥访问。用法是: synchronized(this){/ 区块 /},它的作用域是当前对象。 这时锁就是对象,谁拿到这个锁谁就可以运行它所控制的那段代码。当有一个明确的对象作为锁时,就可以这样写程序,但当没有明确的对象作为锁,只是想让一段代码同步时,可以创建一个特殊的instance变量(它得是一个对象)来充当锁:
class Foo implements Runnable { private byte[] lock = new byte[0]; // 特殊的instance变量 Public void methodA() { synchronized(lock) { //… } } //….. }
注:零长度的byte数组对象创建起来将比任何对象都经济――查看编译后的字节码:生成零长度的byte[]对象只需3条操作码,而Object lock = new Object()则需要7行操作码。
将synchronized作用于static 函数,示例代码如下:
Class Foo { // 同步的static 函数 public synchronized static void methodAAA() { //…. } public void methodBBB() { synchronized(Foo.class) // class literal(类名称字面常量) } }
代码中的methodBBB()方法是把class literal作为锁的情况,它和同步的static函数产生的效果是一样的,取得的锁很特别,是当前调用这个方法的对象所属的类(Class,而不再是由这个Class产生的某个具体对象了)。
可以推断:如果一个类中定义了一个synchronized 的 static 函数A,也定义了一个 synchronized 的 instance函数B,那么这个类的同一对象Obj在多线程中分别访问A和B两个方法时,不会构成同步,因为它们的锁都不一样。B方法的锁是Obj这个对象,而B的锁是Obj所属的那个Class。
在并发环境下,解决共享资源冲突问题时,可以考虑使用锁机制。
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(); } }
原文链接:https://www.cnblogs.com/beiyetengqing/p/6213437.html