synchronized用法
-
修饰代码块
public class SynchronizedTest implements Runnable {
public void test() {
synchronized (this) {
String cur = Thread.currentThread().getName();
for (int i = 0; i < 5; i++) {
System.out.println("current thread is:" + cur + " number is :" + i);
}
}
}
@Override
public void run() {
test();
}
public static void main(String[] args) {
SynchronizedTest test = new SynchronizedTest();
Thread thread1 = new Thread(test, "thread1");
Thread thread2 = new Thread(test, "thread2");
thread1.start();
thread2.start();
}
}
-
修饰方法
public class SynchronizedTest implements Runnable {
public synchronized void test2() {
String cur = Thread.currentThread().getName();
for (int i = 0; i < 5; i ++) {
System.out.println("SynchronizedTest current thread is:" + cur + " number is :" + i);
}
}
@Override
public void run() {
test2();
}
public static void main(String[] args) {
SynchronizedTest test = new SynchronizedTest();
Thread thread1 = new Thread(test, "thread1");
Thread thread2 = new Thread(test, "thread2");
thread1.start();
thread2.start();
}
}
用法总结:
图片粘贴自:https://blog.youkuaiyun.com/u012998254/article/details/82558178
解释一下锁住实例对象和锁住类对象的区别:如果锁住的是实例对象,创建一个实例对象多个线程争锁,只有一个成功;创建俩实例对象通过实例对象创建的线程之间相互不影响,具体如下边代码这种
public static void main(String[] args) {
SynchronizedTest test1 = new SynchronizedTest();
SynchronizedTest test2 = new SynchronizedTest();
Thread thread1 = new Thread(test1, "thread1");
Thread thread2 = new Thread(test2, "thread2");
thread1.start();
thread2.start();
}
如果锁住的是类对象,上边代码执行同一个被锁住的对象也是只能有一个线程能执行:
public void test() {
synchronized (SynchronizedTest.class) {
String cur = Thread.currentThread().getName();
for (int i = 0; i < 5; i++) {
System.out.println("current thread is:" + cur + " number is :" + i);
}
}
}
public static void main(String[] args) {
SynchronizedTest test1 = new SynchronizedTest();
SynchronizedTest test2 = new SynchronizedTest();
Thread thread1 = new Thread(test1, "thread1");
Thread thread2 = new Thread(test2, "thread2");
thread1.start();
thread2.start();
}
current thread is:thread1 number is :0
current thread is:thread1 number is :1
current thread is:thread1 number is :2
current thread is:thread1 number is :3
current thread is:thread1 number is :4
current thread is:thread2 number is :0
current thread is:thread2 number is :1
current thread is:thread2 number is :2
current thread is:thread2 number is :3
current thread is:thread2 number is :4
当方法被 static 修饰的时候,加上 synchronized 关键字是类对象(静态方法属于类,不属于对象);
synchronized 具有可充入性,那么这种场景,类中一个 static 方法加上了 synchronized,另一个成员方法也加上了 synchronized 方法那么线程 a 持有静态方法锁,线程 b 能否访问另一个成员方法?答案是肯定的,这俩 synchronized 不是一个类型锁,一个是类对象锁,一个是实例对象锁【注意!】
synchronized原理分析
java中每个对象都是一个锁,java 创建对象的时候会创建一个 monitor 对象用来标识锁,synchronized 也是通过 monitor 对象实现的,下边的分析主要参考了大神的博客加上自己的理解得到的:https://blog.youkuaiyun.com/javazejian/article/details/72828483
-
monitor结构
ObjectMonitor() {
_header = NULL;
_count = 0; //记录重入次数
_waiters = 0,
_recursions = 0;
_object = NULL;
_owner = NULL;
_WaitSet = NULL; //处于wait状态的线程,会被加入到_WaitSet
_WaitSetLock = 0 ;
_Responsible = NULL ;
_succ = NULL ;
_cxq = NULL ;
FreeNext = NULL ;
_EntryList = NULL ; //处于等待锁block状态的线程,会被加入到该列表
_SpinFreq = 0 ;
_SpinClock = 0 ;
OwnerIsThread = 0 ;
}
-
对象头
synchronized 属于重量级锁,可以看到重量级锁存有指向互斥量的指针,该指针指向了对象所对应的 monitor 对象
-
synchronized 如何实现锁的
上边讲了synchronized 的实现方式主要有两种锁定代码块,锁定方法;这两种方式的区别:
锁定代码块:
锁定代码块主要通过 monitorenter 和 monitorexit 两个指令实现的,当线程执行到 monitorenter 的时候先去对象头找到 monitor 对象的指针,然后找到 monitor 对象,看看 monitor 是否有被线程占用,如果被占用了就进入等待队列,如果没有被占用直接获取锁,monitor 的 count 值增加
//省略其他字节码.......
3: monitorenter //注意此处,进入同步方法(代码块)
****执行代码块结构***
15: monitorexit //注意此处,退出同步方法
****执行代码块结构***
21: monitorexit //注意此处,退出同步方法(多次持有锁)
//省略其他字节码.......
锁定方法:
锁定方法与锁定代码块不同,当线程执行到方法的时候先去方法常量池中的方法表结构(method_info Structure) 中的 ACC_SYNCHRONIZED 访问标志区分一个方法是否同步方法。当方法调用时,调用指令将会 检查方法的 ACC_SYNCHRONIZED 访问标志是否被设置,如果设置了,执行线程将先持有monitor(也是通过对象头找到的), 然后再执行方法,最后再方法完成(无论是正常完成还是非正常完成)时释放monitor
-
synchronized 的优化
synchronized 在 jdk1.6之前属于重型锁,效率较低,后来做了一系列优化性能逐渐提高,所做的优化主要是引入了偏向锁,轻量级锁,重量级锁概念【同样是通过对象头实现的】,具体流程如下:
1. 当线程执行到某个 synchronized修饰的代码块时,锁状态从无锁状态升级为偏向锁(当前线程 Id写到对象头中),可以理解为这个锁专门为这个线程设定,单线程下效率很高
2.当有其他线程请求锁的时候,偏向锁升级为 轻量级锁;线程 a 持有锁,此时线程 b 请求锁,由于锁被 a 持有,线程 b 自旋(避免了线程状态切换),如果自旋一定次数一直没有获得锁当线程 b 挂起,锁升级为重量级锁
3. 当线程 c 来以后直接挂起,等待唤醒
重量级参考:https://blog.youkuaiyun.com/javazejian/article/details/72828483
锁膨胀和优化:https://www.cnblogs.com/aspirant/p/11705068.html