上一讲《多线程——同步(synchronized)下》
多线程——锁(lock)上
首先提出一个问题,synchronized与lock有哪些异同?是性能、适应范围、切入点?还是使用时的复杂度呢?
先了解一下java.util.concurrent.locks.Lock接口的实现类:ReentrantLock与ReentrantReadWriteLock的内部类中的ReadLock与WriteLock;分别叫重入锁,读入锁,写入锁。
而本节中主要讲ReentrantLock的使用与特性。
在ReentrantLock中有这样一段描述:
一个可重入的互斥锁 Lock
,它具有与使用 synchronized
方法和语句所访问的隐式监视器锁相同的一些基本行为和语义,但功能更强大。 (后面的先略过,需要的自己查看API。)
功能更强大? 编写思路:先通过一个例子看看实际效果,再分析ReentrantLock的lock()方法的实现方式,再比较synchronized的异同,最后得出两者的适用范围。
例子:
package thread_test;
import java.util.concurrent.locks.ReentrantLock;
/**
* lock锁
*
* @author ciding
* @createTime Dec 14, 2011 9:54:03 AM
*
*/
public class LockThread {
public static void main(String[] args) {
Long t_start = System.currentTimeMillis();
User_lock u = new User_lock("张三", 100);
Thread_lock t1 = new Thread_lock("线程A", u, 20);
Thread_lock t2 = new Thread_lock("线程B", u, -60);
Thread_lock t3 = new Thread_lock("线程C", u, -80);
Thread_lock t4 = new Thread_lock("线程D", u, -30);
Thread_lock t5 = new Thread_lock("线程E", u, 100);
Thread_lock t6 = new Thread_lock("线程F", u, 50);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
/**
* 以下代码用于计算时间,当然,它本身的运行也会需要一点点时间,但与分析运行效率无影响
*/
boolean flag = true;
while (flag) {
if (Thread_lock.activeCount() == 1) {
Long t_end = System.currentTimeMillis();
System.out.println("当前时间:" + (t_end - t_start));
flag = false;
}
}
}
}
class Thread_lock extends Thread {
private User_lock u;
private int y = 0;
Thread_lock(String name, User_lock u, int y) {
super(name); // 线程的名称
this.u = u;
this.y = y;
}
public void run() {
u.oper(y);
}
}
class User_lock {
private String code;
private Integer cash;
private ReentrantLock myLock1 = new ReentrantLock();
private ReentrantLock myLock2 = new ReentrantLock();
User_lock(String code, int cash) {
this.code = code;
this.cash = cash;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
/**
* 业务方法
*
* @param x
* 添加x万元
*/
public synchronized void oper(int x) {
try{
Thread.sleep(1000);// 作用:增加运行时间
this.cash += x;
System.out.println(Thread.currentThread().getName() + " 运行结束1,增加“"
+ x + "”,当前用户信息:" + toString());
} catch (InterruptedException e) {
e.printStackTrace();
}
try{
Thread.sleep(1000);// 作用:增加运行时间
this.code = "张三(2)";
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return "User{" + "code='" + code + '\'' + ", cash=" + cash + '}';
}
}
例2:
package thread_test;
import java.util.concurrent.locks.ReentrantLock;
/**
* lock锁
*
* @author ciding
* @createTime Dec 14, 2011 9:54:03 AM
*
*/
public class LockThread {
public static void main(String[] args) {
Long t_start = System.currentTimeMillis();
User_lock u = new User_lock("张三", 100);
Thread_lock t1 = new Thread_lock("线程A", u, 20);
Thread_lock t2 = new Thread_lock("线程B", u, -60);
Thread_lock t3 = new Thread_lock("线程C", u, -80);
Thread_lock t4 = new Thread_lock("线程D", u, -30);
Thread_lock t5 = new Thread_lock("线程E", u, 100);
Thread_lock t6 = new Thread_lock("线程F", u, 50);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
/**
* 以下代码用于计算时间,当然,它本身的运行也会需要一点点时间,但与分析运行效率无影响
*/
boolean flag = true;
while (flag) {
if (Thread_lock.activeCount() == 1) {
Long t_end = System.currentTimeMillis();
System.out.println("当前时间:" + (t_end - t_start));
flag = false;
}
}
}
}
class Thread_lock extends Thread {
private User_lock u;
private int y = 0;
Thread_lock(String name, User_lock u, int y) {
super(name); // 线程的名称
this.u = u;
this.y = y;
}
public void run() {
u.oper(y);
}
}
class User_lock {
private String code;
private Integer cash;
private ReentrantLock myLock1 = new ReentrantLock();
private ReentrantLock myLock2 = new ReentrantLock();
User_lock(String code, int cash) {
this.code = code;
this.cash = cash;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
/**
* 业务方法
*
* @param x
* 添加x万元
*/
public void oper(int x) {
synchronized(cash){
try{
Thread.sleep(1000);// 作用:增加运行时间
this.cash += x;
System.out.println(Thread.currentThread().getName() + " 运行结束1,增加“"
+ x + "”,当前用户信息:" + toString());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized(code) {
try{
Thread.sleep(1000);// 作用:增加运行时间
this.code = "张三(2)";
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public String toString() {
return "User{" + "code='" + code + '\'' + ", cash=" + cash + '}';
}
}
例3:
package thread_test;
import java.util.concurrent.locks.ReentrantLock;
/**
* lock锁
*
* @author ciding
* @createTime Dec 14, 2011 9:54:03 AM
*
*/
public class LockThread {
public static void main(String[] args) {
Long t_start = System.currentTimeMillis();
User_lock u = new User_lock("张三", 100);
Thread_lock t1 = new Thread_lock("线程A", u, 20);
Thread_lock t2 = new Thread_lock("线程B", u, -60);
Thread_lock t3 = new Thread_lock("线程C", u, -80);
Thread_lock t4 = new Thread_lock("线程D", u, -30);
Thread_lock t5 = new Thread_lock("线程E", u, 100);
Thread_lock t6 = new Thread_lock("线程F", u, 50);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
/**
* 以下代码用于计算时间,当然,它本身的运行也会需要一点点时间,但与分析运行效率无影响
*/
boolean flag = true;
while (flag) {
if (Thread_lock.activeCount() == 1) {
Long t_end = System.currentTimeMillis();
System.out.println("当前时间:" + (t_end - t_start));
flag = false;
}
}
}
}
class Thread_lock extends Thread {
private User_lock u;
private int y = 0;
Thread_lock(String name, User_lock u, int y) {
super(name); // 线程的名称
this.u = u;
this.y = y;
}
public void run() {
u.oper(y);
}
}
class User_lock {
private String code;
private Integer cash;
private ReentrantLock myLock1 = new ReentrantLock();
private ReentrantLock myLock2 = new ReentrantLock();
User_lock(String code, int cash) {
this.code = code;
this.cash = cash;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
/**
* 业务方法
*
* @param x
* 添加x万元
*/
public void oper(int x) {
myLock1.lock();
try{
Thread.sleep(1000);// 作用:增加运行时间
this.cash += x;
System.out.println(Thread.currentThread().getName() + " 运行结束1,增加“"
+ x + "”,当前用户信息:" + toString());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
myLock1.unlock();
}
myLock2.lock();
try{
Thread.sleep(1000);// 作用:增加运行时间
this.code = "张三(2)";
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
myLock2.unlock();
}
}
@Override
public String toString() {
return "User{" + "code='" + code + '\'' + ", cash=" + cash + '}';
}
}
看上面的时间可以了解一下synchronized与lock的使用在性能上的差别。
单纯的看上面的测试结果:synchronized块是最快的。其实这是因为测试的数据量比较少,线程运行的时间很短。
当运行的线程数以千计的时候,那时对于CPU的上下文切换和调度所消靠的时间就会表现的比较明显。基本上,使用lock会比synchronized块的运行会快一点点,比synchronized方法就快很多了。
按照最开始给的思路,现在应该看看lock()方法的实现了。
/** * Acquires the lock. * *
Acquires the lock if it is not held by another thread and returns * immediately, setting the lock hold count to one. * *
If the current thread already holds the lock then the hold * count is incremented by one and the method returns immediately. * *
If the lock is held by another thread then the * current thread becomes disabled for thread scheduling * purposes and lies dormant until the lock has been acquired, * at which time the lock hold count is set to one. */ public void lock() { sync.lock(); }
直接是通过调用sync的lock()方法。转到sync,发现这个抽象类的lock()方法有两个实现类,分别是NonfairSync与FairSync,刚好对应的一个是非公平锁,一个是公平锁。
非公平锁的代码:加点中文注释
/** * Sync object for non-fair locks */ final static class NonfairSync extends Sync { private static final long serialVersionUID = 7316153563782823691L; /** * Performs lock. Try immediate barge, backing up to normal * acquire on failure. */ final void lock() { //表示如果当前state=0,那么设置state=1,并返回true;否则返回false。由于未等待,所以线程不需加入到等待队列 if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else acquire(1); } protected final boolean tryAcquire(int acquires) { return nonfairTryAcquire(acquires); } }
接下来代码所调用的我只写方法,
acquire(1)->acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) 其中addWaiter负责增加节点
增加完成后selfInterrupt(),设置中断。->tryAcquire(arg)来判断->nonfairTryAcquire(arg)最后回到这个关键方法。
/** * Performs non-fair tryLock. tryAcquire is * implemented in subclasses, but both need nonfair * try for trylock method. */ final boolean nonfairTryAcquire(int acquires) { final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { if (compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) // overflow throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; }
(1)如果锁状态空闲(state=0),且通过原子的比较并设置操作,那么当前线程获得锁,并把当前线程设置为锁拥有者;
(2)如果锁状态空闲,且原子的比较并设置操作失败,那么返回false,说明尝试获得锁失败;
(3)否则,检查当前线程与锁拥有者线程是否相等(表示一个线程已经获得该锁,再次要求该锁,这种情况叫可重入锁),如果相等,维护锁状态,并返回true;
(4)如果不是以上情况,说明锁已经被其他的线程持有,直接返回false;
到此,核心代码就分析完了。
引用一段别人的总结:
先到这里,需要写的东西太多,突然有点下不了手的感觉
ReentrantLock对synchronized的性能改进:
ReentrantLock是通过实现一个接口Lock,把锁定做为了一java类来实现,而不是做为语言的特性;从而可能实现不同的调度算法、性能特性等。而这里,ReentrantLock拥有synchronized相同的并发性与内存语义,另外,又添加了类似锁投票,定时锁等候,可中断锁等候的一些特性。
ReentrantLock对synchronized的可伸缩改进:
在非公平锁中,CPU的处理时间中,只有很少的时间花在线程调度上,大多都用在实际工作上。
怕越写越深,到我都控制不了的时候,就不好了;所以,最后还是用一个“围城”的比喻来说明synchronized与lock的差异来做为总结。
当我们看lock处处都比synchronized好的时候,请看下面:
在使用lock方法的时候,应该注意到一点,就是finally后面要加上unlock()方法,这个就有点像Java与C++在内存回收上的区别,所以写代码的时候,就要特别小心;另外,因为Lock是一个接口,它的实现也就是一个普通的类而已,所以JVM在生成线程转储时能够包括锁定信息的时候,synchronized可以指定到代码,而Lock就不行了,这对于调试,你懂的。
围城效应就在这两者之间来回的跑,就想当年喜欢内存回收之后,又去恶补JVM的内存回收机制是一样的。
Java多线程及线程池专题 汇总http://ciding.iteye.com/blog/1300110