Ubuntu编译glibc库来验证Synchronized锁优化效果

0.前言


当然你可以说通过JOL查看对象的头信息,当然那个也是一种方法,但那个只能说明是这种状态,没有过程,本次主要结合来验证,Synchronized 关键字锁的状态:无锁、偏向锁、轻量级锁、重量级锁

基于Ubuntu16测试成功。18测试失败,centos没有测试




主要是在Java调用系统函数的时候,打印log。但是在Java里面打印线程ID是JVM层面的。需要自己实现一个native方法,在java层面调用。最后对比对应的PID 和TID,发现查看当前线程调用了几次系统锁
Java 的重量级级锁都是通过glibc库的pthread_mutex_lock函数实现的,是在和操作系统级别的锁。

glibc库


glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc。glibc除了封装linux操作系统所提供的系统服务外,它本身也提供了许多其它一些必要功能服务的实现。

  • 操作系统底层库

int pthread_mutex_lock(pthread_mutex_t *mutex);


pthread_mutex_lock()返回时,该互斥锁已被锁定。线程调用该函数让互斥锁上锁,如果该互斥锁已被另一个线程锁定和拥有,则调用该线程将阻塞,直到该互斥锁变为可用为止。 对于 Solaris线程,请参见mutex_lock 语法

  • 会阻塞


我们可以从JVM 源码中看到调用

ThreadCritical::ThreadCritical

1
2
3
4
5
6
7
8
9
10
ThreadCritical::ThreadCritical() {
pthread_t self = pthread_self();
if (self != tc_owner) {
int ret = pthread_mutex_lock(&tc_mutex);
guarantee(ret == 0, "fatal error with pthread_mutex_lock()");
assert(tc_count == 0, "Lock acquired with illegal reentry count.");
tc_owner = self;
}
tc_count++;
}


这个是底层系统库,我们是看不到源码的,只能自己下载。可以在glibc/nptl/phread_mutex_lock.c里看到实现。
我尝试就是在这里函数里加上自己的log

1
2
3
4
5
int __pthread_mutex_lock (pthread_mutex_t *mutex)
{
//打印log
fprintf(stdout,"pthread_mutex_lock log PID = %d TID = %lu mutex = %p \n",getpid(),pthread_self(),&mutex);
---

1. 查看自己的glibc版本

1
2
3
4
5
[email protected]:/lib/x86_64-linux-gnu# pwd
/lib/x86_64-linux-gnu
[email protected]:/lib/x86_64-linux-gnu# ls -l libc.so.6
lrwxrwxrwx 1 root root 12 Feb 5 2019 libc.so.6 -> libc-2.23.so
[email protected]:/lib/x86_64-linux-gnu#

2.最好下载同一个版本,编译glibc版本


https://ftp.gnu.org/gnu/glibc/?C=M;O=A
目录可以自己变化,但是glibc不让在源码目录下面编译 会报 Makeconfig:42: *** missing separator. Stop




若有必要,可以参考 Linux源码包的一般安装步

1
2
3
4
5
mkdir build
cd build
./../configure --prefix = usr/local/glibc
make
make install

3.修改替换 编译


vim pthread_mutex_lock.c 添加log

1
fprintf(stdout,"pthread_mutex_lock log  PID = %d TID = %lu  mutex = %p \n",getpid(),pthread_self(),&mutex);

4.运行时替换关键库

  • 这里需要注意,搞错了,就不能的话,所有命令都可能用不了了


自己主要修改的是pthread库,所以替换系统的pthread库,find一下

  • 1.查看 /etc/ld.conf.d/x86-gnu*.conf 下的目录
  • 2.因为要替换几个关键文件,直接手动替换会出错,系统库是在动态加载的。copy 一个临时目录,加上了n后缀,做区分。去替换临时目录里面的文件
  • 3.例如我的
1
2
3
4
5
cp /usr/local/glibc23/lib/libpthread.so /usr/lib/x86_64-linux-gnu-n/libpthread.so
cp /usr/local/glibc23/lib/libpthread_nonshared.a /usr/lib/x86_64-linux-gnu-n/libpthread_nonshared.a
cp /usr/local/glibc23/lib/libpthread.a /usr/lib/x86_64-linux-gnu-n/libpthread.a
cp /usr/local/glibc23/lib/libpthread-2.23.so /lib/x86_64-linux-gnu-n/libpthread-2.23.so
cp /usr/local/glibc23/lib/libpthread.so.0 /lib/x86_64-linux-gnu-n/libpthread.so.0
  • 4.修改 /etc/ld.conf.d/x86-gnu*.conf 里面的链接库路径指向临时目录,不用了可以改回
  • 5.执行ldconfig,生效
  • 6.自己配置java环境
  • 基本任命令都会触发这行log
1
2
3
[email protected]:/etc/ld.so.conf.d# ls
pthread_mutex_lock log PID = 62030 TID = 140453473769472 mutex=0x7fbddf6a1948
libc.conf x86_64-linux-gnu.conf

5.编写Java验证


接下来,就是正常jni流程了

JniTool.java

1
2
3
4
5
6
7
8
package jnitool;

public class JniTool {
static {
System.loadLibrary("jnitools");
}
public native void getThreadID();
}


这里jnitool必须在 java.library.path这一jvm变量所指向的路径中,
可以通过如下方法获得该变量System.getProperty(“java.library.path”);

生成.h文件,javah -jni jnitool.JniTool


打印java线程的pid和tid,jvm层面

1
2
3
4
5
6
7
8
9
10
#include "jnitool_JniTool.h"
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

JNIEXPORT void JNICALL Java_jnitool_JniTool_getThreadID
(JNIEnv * env, jobject obj){
fprintf(stdout,"java_lock log PID = %d TID = %lu \n",getpid(),pthread_self());
fflush(stdout);//刷新,防止错位打印
}

使用make编译

1
2
3
4
5
6
vim makefile

libjnitools.so:
gcc -lpthread -fPIC -shared -o libjnitools.so -I /root/jdk1.8.0_221/include/ -I /root/jdk1.8.0_221/include/linux/ ./jnitools.c

make

6. 观察不同的锁

  • 主要是对比不同的锁情况下,对操作系统系统锁的调用情况,观察优化效果
  • 当然JVM对锁的优化,是调用系统锁的次数越少越好
  • 听过系统锁的log,直观对比调用pthread_mutex_lock次数
  • 奇怪的是我在docker里面,修改glibc库后,加上jol打印头信息就会卡,所以先验证是什么锁的情况下,再打的log

6.1 无锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class SyncTest {
//测试锁
final Object lock = new Object();
//获取pthread tid
JniTool jniTool = new JniTool();

public static void main(String[] args) throws InterruptedException {
//不打开,默认就是无所的状态
//Thread.sleep(5000);
SyncTest test = new SyncTest();
System.out.println("---------------------------start--------------------");
test.start();
}

public void start() throws InterruptedException {
Runnable runnable = new Runnable() {
@Override
public void run() {
int count = 0;
System.out.println(Thread.currentThread().getName() + " start");
while (true) {
if (count < 50) {
count++;
} else {
return;
}
try {
lock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();
}
public void lock() throws InterruptedException {
synchronized (lock) {
jniTool.getThreadID();
}
}
}

out.log

  • 通过log可以看到50次调用,通过搜索tid查看

  • 刚开始和结束调用了系统锁,中间是没有调用系统锁

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    3545: pthread_mutex_lock log  PID = 399 TID = 140058569967360 mutex=0x7f61e800b728 
    3546: pthread_mutex_lock log PID = 399 TID = 140058569967360 mutex=0x7f61e8100b28
    3549: pthread_mutex_lock log PID = 399 TID = 140058569967360 mutex=0x7f61e8100b28
    3555: pthread_mutex_lock log PID = 399 TID = 140058569967360 mutex=0x7f61e80b9228
    3562: pthread_mutex_lock log PID = 399 TID = 140058569967360 mutex=0x7f61f778b8a0
    3563: pthread_mutex_lock log PID = 399 TID = 140058569967360 mutex=0x7f61f83d3948
    3564: java_lock log-----------PID = 399 TID = 140058569967360
    3575: java_lock log-----------PID = 399 TID = 140058569967360
    3576: java_lock log-----------PID = 399 TID = 140058569967360
    3577: java_lock log-----------PID = 399 TID = 140058569967360
    3578: java_lock log-----------PID = 399 TID = 140058569967360
    3579: java_lock log-----------PID = 399 TID = 140058569967360
    3580: java_lock log-----------PID = 399 TID = 140058569967360
    3581: java_lock log-----------PID = 399 TID = 140058569967360
    3582: java_lock log-----------PID = 399 TID = 140058569967360
    3583: java_lock log-----------PID = 399 TID = 140058569967360
    3584: java_lock log-----------PID = 399 TID = 140058569967360
    3585: java_lock log-----------PID = 399 TID = 140058569967360
    3586: java_lock log-----------PID = 399 TID = 140058569967360
    3587: java_lock log-----------PID = 399 TID = 140058569967360
    3588: java_lock log-----------PID = 399 TID = 140058569967360
    3590: java_lock log-----------PID = 399 TID = 140058569967360
    3591: java_lock log-----------PID = 399 TID = 140058569967360
    3592: java_lock log-----------PID = 399 TID = 140058569967360
    3593: java_lock log-----------PID = 399 TID = 140058569967360
    3594: java_lock log-----------PID = 399 TID = 140058569967360
    3595: java_lock log-----------PID = 399 TID = 140058569967360
    3596: java_lock log-----------PID = 399 TID = 140058569967360
    3597: java_lock log-----------PID = 399 TID = 140058569967360
    3598: java_lock log-----------PID = 399 TID = 140058569967360
    3599: java_lock log-----------PID = 399 TID = 140058569967360
    3600: java_lock log-----------PID = 399 TID = 140058569967360
    3601: java_lock log-----------PID = 399 TID = 140058569967360
    3602: java_lock log-----------PID = 399 TID = 140058569967360
    3603: java_lock log-----------PID = 399 TID = 140058569967360
    3604: java_lock log-----------PID = 399 TID = 140058569967360
    3605: java_lock log-----------PID = 399 TID = 140058569967360
    3606: java_lock log-----------PID = 399 TID = 140058569967360
    3607: java_lock log-----------PID = 399 TID = 140058569967360
    3608: java_lock log-----------PID = 399 TID = 140058569967360
    3609: java_lock log-----------PID = 399 TID = 140058569967360
    3610: java_lock log-----------PID = 399 TID = 140058569967360
    3611: java_lock log-----------PID = 399 TID = 140058569967360
    3612: java_lock log-----------PID = 399 TID = 140058569967360
    3613: java_lock log-----------PID = 399 TID = 140058569967360
    3614: java_lock log-----------PID = 399 TID = 140058569967360
    3615: java_lock log-----------PID = 399 TID = 140058569967360
    3616: java_lock log-----------PID = 399 TID = 140058569967360
    3617: java_lock log-----------PID = 399 TID = 140058569967360
    3618: java_lock log-----------PID = 399 TID = 140058569967360
    3619: java_lock log-----------PID = 399 TID = 140058569967360
    3620: java_lock log-----------PID = 399 TID = 140058569967360
    3621: java_lock log-----------PID = 399 TID = 140058569967360
    3622: java_lock log-----------PID = 399 TID = 140058569967360
    3623: java_lock log-----------PID = 399 TID = 140058569967360
    3624: pthread_mutex_lock log PID = 399 TID = 140058569967360 mutex=0x7f61e800b528
    3625: pthread_mutex_lock log PID = 399 TID = 140058569967360 mutex=0x7f61f77a32c0
    3626: pthread_mutex_lock log PID = 399 TID = 140058569967360 mutex=0x7f61f77a32c0

    6.2 偏向锁

  • 偏向锁是默认延时加载的

  • 我们可以加JVM参数或sleep

  • 通过上面的代码,加上 Thread.sleep(5000);确保偏向锁打开


    out.log

  • 查看输出, 效果如上

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    3742: pthread_mutex_lock log  PID = 431 TID = 139799890413312 mutex=0x7f25c800b728 
    3743: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25c80e9128
    3750: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25c80e9128
    3752: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25c80b9228
    3763: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25cffe18a0
    3764: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25d4c8b948
    3765: java_lock log-----------PID = 431 TID = 139799890413312
    3772: java_lock log-----------PID = 431 TID = 139799890413312
    3773: java_lock log-----------PID = 431 TID = 139799890413312
    3774: java_lock log-----------PID = 431 TID = 139799890413312
    3775: java_lock log-----------PID = 431 TID = 139799890413312
    3776: java_lock log-----------PID = 431 TID = 139799890413312
    3777: java_lock log-----------PID = 431 TID = 139799890413312
    3778: java_lock log-----------PID = 431 TID = 139799890413312
    3779: java_lock log-----------PID = 431 TID = 139799890413312
    3780: java_lock log-----------PID = 431 TID = 139799890413312
    3781: java_lock log-----------PID = 431 TID = 139799890413312
    3782: java_lock log-----------PID = 431 TID = 139799890413312
    3783: java_lock log-----------PID = 431 TID = 139799890413312
    3785: java_lock log-----------PID = 431 TID = 139799890413312
    3787: java_lock log-----------PID = 431 TID = 139799890413312
    3788: java_lock log-----------PID = 431 TID = 139799890413312
    3789: java_lock log-----------PID = 431 TID = 139799890413312
    3790: java_lock log-----------PID = 431 TID = 139799890413312
    3791: java_lock log-----------PID = 431 TID = 139799890413312
    3792: java_lock log-----------PID = 431 TID = 139799890413312
    3793: java_lock log-----------PID = 431 TID = 139799890413312
    3794: java_lock log-----------PID = 431 TID = 139799890413312
    3795: java_lock log-----------PID = 431 TID = 139799890413312
    3796: java_lock log-----------PID = 431 TID = 139799890413312
    3797: java_lock log-----------PID = 431 TID = 139799890413312
    3798: java_lock log-----------PID = 431 TID = 139799890413312
    3799: java_lock log-----------PID = 431 TID = 139799890413312
    3800: java_lock log-----------PID = 431 TID = 139799890413312
    3801: java_lock log-----------PID = 431 TID = 139799890413312
    3802: java_lock log-----------PID = 431 TID = 139799890413312
    3803: java_lock log-----------PID = 431 TID = 139799890413312
    3804: java_lock log-----------PID = 431 TID = 139799890413312
    3805: java_lock log-----------PID = 431 TID = 139799890413312
    3806: java_lock log-----------PID = 431 TID = 139799890413312
    3807: java_lock log-----------PID = 431 TID = 139799890413312
    3808: java_lock log-----------PID = 431 TID = 139799890413312
    3809: java_lock log-----------PID = 431 TID = 139799890413312
    3810: java_lock log-----------PID = 431 TID = 139799890413312
    3811: java_lock log-----------PID = 431 TID = 139799890413312
    3812: java_lock log-----------PID = 431 TID = 139799890413312
    3813: java_lock log-----------PID = 431 TID = 139799890413312
    3814: java_lock log-----------PID = 431 TID = 139799890413312
    3815: java_lock log-----------PID = 431 TID = 139799890413312
    3816: java_lock log-----------PID = 431 TID = 139799890413312
    3817: java_lock log-----------PID = 431 TID = 139799890413312
    3818: java_lock log-----------PID = 431 TID = 139799890413312
    3819: java_lock log-----------PID = 431 TID = 139799890413312
    3820: java_lock log-----------PID = 431 TID = 139799890413312
    3821: java_lock log-----------PID = 431 TID = 139799890413312
    3822: java_lock log-----------PID = 431 TID = 139799890413312
    3823: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25c8079528
    3824: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25c80e9128
    3828: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25c80e9128
    3829: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25c800b528
    3830: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25cfff92c0
    3831: pthread_mutex_lock log PID = 431 TID = 139799890413312 mutex=0x7f25cfff92c0

6.3 轻量级锁

  • 我们偏向锁,升级到轻量级锁
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    public class SyncTest {
    //测试锁
    final Object lock = new Object();
    //获取pthread tid
    JniTool jniTool = new JniTool();

    public static void main(String[] args) throws InterruptedException {
    //打开就是默认的偏向锁,默认是可偏向状态,但是不偏向任何一个线程
    //不打开,默认就是无所的状态
    Thread.sleep(5000);
    SyncTest test = new SyncTest();
    // test.parseHeaderInfo((ClassLayout.parseInstance(test.lock).toPrintable()));
    System.out.println("---------------------------start--------------------");
    test.start();
    }

    public void start() throws InterruptedException {
    Runnable runnable = new Runnable() {
    @Override
    public void run() {
    int count = 0;
    System.out.println(Thread.currentThread().getName() + " start");
    while (true) {
    if (count < 50) {
    count++;
    } else {
    //若线程没有结束,但是线程1放锁了,升级为轻量级锁
    try {
    Thread.sleep(100000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    return;
    }
    try {
    lock();
    // parseHeaderInfo(ClassLayout.parseInstance(lock).toPrintable());
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    };
    new Thread(runnable).start();
    //加上,保证线程1结束,若果线程1已经结束,则还是偏向锁
    //若线程没有结束,但是线程1放锁了,升级为轻量级锁
    Thread.sleep(2000);
    //不加,则会升级为重量级锁
    new Thread(runnable).start();
    }
    public void lock() throws InterruptedException {
    synchronized (lock) {
    jniTool.getThreadID();
    }
    }
    }
  • out.log
  • 线程1的时候偏向
  • 线程2的时候,升级到轻量级锁
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    3763: pthread_mutex_lock log  PID = 498 TID = 139928822331136 mutex=0x7f43cc00b728 
    3764: pthread_mutex_lock log PID = 498 TID = 139928822331136 mutex=0x7f43cc108d28
    3768: pthread_mutex_lock log PID = 498 TID = 139928822331136 mutex=0x7f43cc108d28
    3769: pthread_mutex_lock log PID = 498 TID = 139928822331136 mutex=0x7f43cc0bba28
    3774: pthread_mutex_lock log PID = 498 TID = 139928822331136 mutex=0x7f43d3fe18a0
    3775: pthread_mutex_lock log PID = 498 TID = 139928822331136 mutex=0x7f43d9bea948
    3777: java_lock log-----------PID = 498 TID = 139928822331136
    3789: java_lock log-----------PID = 498 TID = 139928822331136
    3790: java_lock log-----------PID = 498 TID = 139928822331136
    3791: java_lock log-----------PID = 498 TID = 139928822331136
    3792: java_lock log-----------PID = 498 TID = 139928822331136
    3793: java_lock log-----------PID = 498 TID = 139928822331136
    3794: java_lock log-----------PID = 498 TID = 139928822331136
    3795: java_lock log-----------PID = 498 TID = 139928822331136
    3796: java_lock log-----------PID = 498 TID = 139928822331136
    3797: java_lock log-----------PID = 498 TID = 139928822331136
    3798: java_lock log-----------PID = 498 TID = 139928822331136
    3799: java_lock log-----------PID = 498 TID = 139928822331136
    3800: java_lock log-----------PID = 498 TID = 139928822331136
    3801: java_lock log-----------PID = 498 TID = 139928822331136
    3802: java_lock log-----------PID = 498 TID = 139928822331136
    3803: java_lock log-----------PID = 498 TID = 139928822331136
    3804: java_lock log-----------PID = 498 TID = 139928822331136
    3805: java_lock log-----------PID = 498 TID = 139928822331136
    3806: java_lock log-----------PID = 498 TID = 139928822331136
    3807: java_lock log-----------PID = 498 TID = 139928822331136
    3808: java_lock log-----------PID = 498 TID = 139928822331136
    3809: java_lock log-----------PID = 498 TID = 139928822331136
    3810: java_lock log-----------PID = 498 TID = 139928822331136
    3811: java_lock log-----------PID = 498 TID = 139928822331136
    3812: java_lock log-----------PID = 498 TID = 139928822331136
    3813: java_lock log-----------PID = 498 TID = 139928822331136
    3814: java_lock log-----------PID = 498 TID = 139928822331136
    3815: java_lock log-----------PID = 498 TID = 139928822331136
    3816: java_lock log-----------PID = 498 TID = 139928822331136
    3817: java_lock log-----------PID = 498 TID = 139928822331136
    3818: java_lock log-----------PID = 498 TID = 139928822331136
    3819: java_lock log-----------PID = 498 TID = 139928822331136
    3820: java_lock log-----------PID = 498 TID = 139928822331136
    3822: java_lock log-----------PID = 498 TID = 139928822331136
    3824: java_lock log-----------PID = 498 TID = 139928822331136
    3825: java_lock log-----------PID = 498 TID = 139928822331136
    3826: java_lock log-----------PID = 498 TID = 139928822331136
    3827: java_lock log-----------PID = 498 TID = 139928822331136
    3828: java_lock log-----------PID = 498 TID = 139928822331136
    3829: java_lock log-----------PID = 498 TID = 139928822331136
    3830: java_lock log-----------PID = 498 TID = 139928822331136
    3831: java_lock log-----------PID = 498 TID = 139928822331136
    3832: java_lock log-----------PID = 498 TID = 139928822331136
    3833: java_lock log-----------PID = 498 TID = 139928822331136
    3834: java_lock log-----------PID = 498 TID = 139928822331136
    3835: java_lock log-----------PID = 498 TID = 139928822331136
    3836: java_lock log-----------PID = 498 TID = 139928822331136
    3837: java_lock log-----------PID = 498 TID = 139928822331136
    3838: java_lock log-----------PID = 498 TID = 139928822331136
    3839: java_lock log-----------PID = 498 TID = 139928822331136
    3840: pthread_mutex_lock log PID = 498 TID = 139928822331136 mutex=0x7f43cc108b28
    3932: pthread_mutex_lock log PID = 498 TID = 139928821278464 mutex=0x7f43cc00b728
    3933: pthread_mutex_lock log PID = 498 TID = 139928821278464 mutex=0x7f43cc0dd228
    3938: pthread_mutex_lock log PID = 498 TID = 139928821278464 mutex=0x7f43cc0dd228
    3942: pthread_mutex_lock log PID = 498 TID = 139928821278464 mutex=0x7f43cc079528
    3945: pthread_mutex_lock log PID = 498 TID = 139928821278464 mutex=0x7f43cc079528
    3950: pthread_mutex_lock log PID = 498 TID = 139928821278464 mutex=0x7f43cc079528
    3953: java_lock log-----------PID = 498 TID = 139928821278464
    3954: java_lock log-----------PID = 498 TID = 139928821278464
    3955: java_lock log-----------PID = 498 TID = 139928821278464
    3956: java_lock log-----------PID = 498 TID = 139928821278464
    3957: java_lock log-----------PID = 498 TID = 139928821278464
    3958: java_lock log-----------PID = 498 TID = 139928821278464
    3959: java_lock log-----------PID = 498 TID = 139928821278464
    3960: java_lock log-----------PID = 498 TID = 139928821278464
    3961: java_lock log-----------PID = 498 TID = 139928821278464
    3962: java_lock log-----------PID = 498 TID = 139928821278464
    3963: java_lock log-----------PID = 498 TID = 139928821278464
    3964: java_lock log-----------PID = 498 TID = 139928821278464
    3965: java_lock log-----------PID = 498 TID = 139928821278464
    3966: java_lock log-----------PID = 498 TID = 139928821278464
    3967: java_lock log-----------PID = 498 TID = 139928821278464
    3968: java_lock log-----------PID = 498 TID = 139928821278464
    3969: java_lock log-----------PID = 498 TID = 139928821278464
    3970: java_lock log-----------PID = 498 TID = 139928821278464
    3971: java_lock log-----------PID = 498 TID = 139928821278464
    3972: java_lock log-----------PID = 498 TID = 139928821278464
    3973: java_lock log-----------PID = 498 TID = 139928821278464
    3974: java_lock log-----------PID = 498 TID = 139928821278464
    3975: java_lock log-----------PID = 498 TID = 139928821278464
    3976: java_lock log-----------PID = 498 TID = 139928821278464
    3977: java_lock log-----------PID = 498 TID = 139928821278464
    3978: java_lock log-----------PID = 498 TID = 139928821278464
    3979: java_lock log-----------PID = 498 TID = 139928821278464
    3980: java_lock log-----------PID = 498 TID = 139928821278464
    3981: java_lock log-----------PID = 498 TID = 139928821278464
    3982: java_lock log-----------PID = 498 TID = 139928821278464
    3983: java_lock log-----------PID = 498 TID = 139928821278464
    3984: java_lock log-----------PID = 498 TID = 139928821278464
    3985: java_lock log-----------PID = 498 TID = 139928821278464
    3986: java_lock log-----------PID = 498 TID = 139928821278464
    3987: java_lock log-----------PID = 498 TID = 139928821278464
    3988: java_lock log-----------PID = 498 TID = 139928821278464
    3989: java_lock log-----------PID = 498 TID = 139928821278464
    3990: java_lock log-----------PID = 498 TID = 139928821278464
    3991: java_lock log-----------PID = 498 TID = 139928821278464
    3992: java_lock log-----------PID = 498 TID = 139928821278464
    3993: java_lock log-----------PID = 498 TID = 139928821278464
    3994: java_lock log-----------PID = 498 TID = 139928821278464
    3995: java_lock log-----------PID = 498 TID = 139928821278464
    3996: java_lock log-----------PID = 498 TID = 139928821278464
    3997: java_lock log-----------PID = 498 TID = 139928821278464
    3998: java_lock log-----------PID = 498 TID = 139928821278464
    3999: java_lock log-----------PID = 498 TID = 139928821278464
    4000: java_lock log-----------PID = 498 TID = 139928821278464
    4001: java_lock log-----------PID = 498 TID = 139928821278464
    4002: java_lock log-----------PID = 498 TID = 139928821278464
    4003: pthread_mutex_lock log PID = 498 TID = 139928821278464 mutex=0x7f43cc0dd028

6.4 重量级锁

  • 上面的代码,我们2个线程不等待,直接启动
  • 则会再偏向锁的情况下,直接升级到重量级锁

out.log

  • 可以明显看到 系统锁的调用测试变多

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    3750: pthread_mutex_lock log  PID = 532 TID = 139648716183296 mutex=0x7f029000b728 
    3751: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f0290108c28
    3756: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f0290108c28
    3758: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029000b728
    3759: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f02900b9228
    3760: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a528
    3770: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a528
    3775: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029d4b28a0
    3776: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029e0fa948
    3778: java_lock log-----------PID = 532 TID = 139648716183296
    3784: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f0290079528
    3785: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a528
    3796: java_lock log-----------PID = 532 TID = 139648716183296
    3797: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a528
    3798: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3799: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3800: java_lock log-----------PID = 532 TID = 139648716183296
    3801: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3802: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3803: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3804: java_lock log-----------PID = 532 TID = 139648716183296
    3805: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3806: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3807: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3808: java_lock log-----------PID = 532 TID = 139648716183296
    3809: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3810: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3811: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3812: java_lock log-----------PID = 532 TID = 139648716183296
    3813: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3814: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3815: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3816: java_lock log-----------PID = 532 TID = 139648716183296
    3817: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3818: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3819: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3820: java_lock log-----------PID = 532 TID = 139648716183296
    3821: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3822: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3823: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3824: java_lock log-----------PID = 532 TID = 139648716183296
    3825: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3826: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3827: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3828: java_lock log-----------PID = 532 TID = 139648716183296
    3829: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3830: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3831: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3832: java_lock log-----------PID = 532 TID = 139648716183296
    3833: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3834: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3835: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3836: java_lock log-----------PID = 532 TID = 139648716183296
    3837: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3838: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3839: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3840: java_lock log-----------PID = 532 TID = 139648716183296
    3841: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3842: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3843: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3844: java_lock log-----------PID = 532 TID = 139648716183296
    3845: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3846: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3847: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3848: java_lock log-----------PID = 532 TID = 139648716183296
    3849: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3850: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3851: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3852: java_lock log-----------PID = 532 TID = 139648716183296
    3853: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3854: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3855: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3856: java_lock log-----------PID = 532 TID = 139648716183296
    3857: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3858: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3859: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3860: java_lock log-----------PID = 532 TID = 139648716183296
    3861: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3862: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3863: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3864: java_lock log-----------PID = 532 TID = 139648716183296
    3865: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3866: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3867: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3868: java_lock log-----------PID = 532 TID = 139648716183296
    3869: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3870: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3871: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3872: java_lock log-----------PID = 532 TID = 139648716183296
    3873: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3874: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3875: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3876: java_lock log-----------PID = 532 TID = 139648716183296
    3877: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3878: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3879: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3880: java_lock log-----------PID = 532 TID = 139648716183296
    3881: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3882: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3883: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3884: java_lock log-----------PID = 532 TID = 139648716183296
    3885: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3886: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3887: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3888: java_lock log-----------PID = 532 TID = 139648716183296
    3889: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3890: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3891: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3892: java_lock log-----------PID = 532 TID = 139648716183296
    3893: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3894: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3895: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3896: java_lock log-----------PID = 532 TID = 139648716183296
    3897: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3898: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3899: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3900: java_lock log-----------PID = 532 TID = 139648716183296
    3901: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3902: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3903: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3904: java_lock log-----------PID = 532 TID = 139648716183296
    3905: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3906: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3907: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3908: java_lock log-----------PID = 532 TID = 139648716183296
    3909: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3910: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3911: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3912: java_lock log-----------PID = 532 TID = 139648716183296
    3913: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3914: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3915: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3916: java_lock log-----------PID = 532 TID = 139648716183296
    3917: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3918: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3919: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3920: java_lock log-----------PID = 532 TID = 139648716183296
    3921: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3922: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3923: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3924: java_lock log-----------PID = 532 TID = 139648716183296
    3925: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3926: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3927: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3928: java_lock log-----------PID = 532 TID = 139648716183296
    3929: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3930: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3931: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3932: java_lock log-----------PID = 532 TID = 139648716183296
    3933: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3934: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3935: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3936: java_lock log-----------PID = 532 TID = 139648716183296
    3937: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3938: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3939: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3940: java_lock log-----------PID = 532 TID = 139648716183296
    3941: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3942: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3943: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3944: java_lock log-----------PID = 532 TID = 139648716183296
    3945: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3946: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3947: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3948: java_lock log-----------PID = 532 TID = 139648716183296
    3949: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3950: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3951: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3952: java_lock log-----------PID = 532 TID = 139648716183296
    3953: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3954: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3955: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3956: java_lock log-----------PID = 532 TID = 139648716183296
    3957: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3958: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3959: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3960: java_lock log-----------PID = 532 TID = 139648716183296
    3961: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3962: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3963: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3964: java_lock log-----------PID = 532 TID = 139648716183296
    3965: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3966: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3967: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3968: java_lock log-----------PID = 532 TID = 139648716183296
    3969: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3970: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3971: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3972: java_lock log-----------PID = 532 TID = 139648716183296
    3973: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3974: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3975: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3976: java_lock log-----------PID = 532 TID = 139648716183296
    3977: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3978: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3979: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3980: java_lock log-----------PID = 532 TID = 139648716183296
    3981: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3982: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3983: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3984: java_lock log-----------PID = 532 TID = 139648716183296
    3985: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3986: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3987: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3988: java_lock log-----------PID = 532 TID = 139648716183296
    3989: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3990: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3991: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f029010a128
    3992: pthread_mutex_lock log PID = 532 TID = 139648716183296 mutex=0x7f0290108a28
    3993: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a128
    3994: java_lock log-----------PID = 532 TID = 139648647034624
    3995: java_lock log-----------PID = 532 TID = 139648647034624
    3996: java_lock log-----------PID = 532 TID = 139648647034624
    3997: java_lock log-----------PID = 532 TID = 139648647034624
    3998: java_lock log-----------PID = 532 TID = 139648647034624
    3999: java_lock log-----------PID = 532 TID = 139648647034624
    4000: java_lock log-----------PID = 532 TID = 139648647034624
    4001: java_lock log-----------PID = 532 TID = 139648647034624
    4002: java_lock log-----------PID = 532 TID = 139648647034624
    4003: java_lock log-----------PID = 532 TID = 139648647034624
    4004: java_lock log-----------PID = 532 TID = 139648647034624
    4005: java_lock log-----------PID = 532 TID = 139648647034624
    4006: java_lock log-----------PID = 532 TID = 139648647034624
    4007: java_lock log-----------PID = 532 TID = 139648647034624
    4008: java_lock log-----------PID = 532 TID = 139648647034624
    4009: java_lock log-----------PID = 532 TID = 139648647034624
    4010: java_lock log-----------PID = 532 TID = 139648647034624
    4011: java_lock log-----------PID = 532 TID = 139648647034624
    4012: java_lock log-----------PID = 532 TID = 139648647034624
    4013: java_lock log-----------PID = 532 TID = 139648647034624
    4014: java_lock log-----------PID = 532 TID = 139648647034624
    4015: java_lock log-----------PID = 532 TID = 139648647034624
    4016: java_lock log-----------PID = 532 TID = 139648647034624
    4017: java_lock log-----------PID = 532 TID = 139648647034624
    4018: java_lock log-----------PID = 532 TID = 139648647034624
    4019: java_lock log-----------PID = 532 TID = 139648647034624
    4020: java_lock log-----------PID = 532 TID = 139648647034624
    4021: java_lock log-----------PID = 532 TID = 139648647034624
    4022: java_lock log-----------PID = 532 TID = 139648647034624
    4023: java_lock log-----------PID = 532 TID = 139648647034624
    4024: java_lock log-----------PID = 532 TID = 139648647034624
    4025: java_lock log-----------PID = 532 TID = 139648647034624
    4026: java_lock log-----------PID = 532 TID = 139648647034624
    4027: java_lock log-----------PID = 532 TID = 139648647034624
    4028: java_lock log-----------PID = 532 TID = 139648647034624
    4029: java_lock log-----------PID = 532 TID = 139648647034624
    4030: java_lock log-----------PID = 532 TID = 139648647034624
    4031: java_lock log-----------PID = 532 TID = 139648647034624
    4032: java_lock log-----------PID = 532 TID = 139648647034624
    4033: java_lock log-----------PID = 532 TID = 139648647034624
    4034: java_lock log-----------PID = 532 TID = 139648647034624
    4035: java_lock log-----------PID = 532 TID = 139648647034624
    4036: java_lock log-----------PID = 532 TID = 139648647034624
    4037: java_lock log-----------PID = 532 TID = 139648647034624
    4038: java_lock log-----------PID = 532 TID = 139648647034624
    4039: java_lock log-----------PID = 532 TID = 139648647034624
    4040: java_lock log-----------PID = 532 TID = 139648647034624
    4041: java_lock log-----------PID = 532 TID = 139648647034624
    4042: java_lock log-----------PID = 532 TID = 139648647034624
    4043: java_lock log-----------PID = 532 TID = 139648647034624
    4044: pthread_mutex_lock log PID = 532 TID = 139648647034624 mutex=0x7f029010a328

    7 总结

  • 无锁,偏向锁,轻量级锁,本质上都JVM层面的锁,除了开始和结束的时候,没有调用系统锁。

  • 重量级锁是需要 系统锁 pthread_mutex_lock 支持的

  • 每个线程50次调用,系统锁的调用次数,我们包括开始和结束

无锁 偏向 轻量级 重量级
线程数量 1 1 2 2
pthread_mutex_lock 调用次数 12 次 12次 14 次 161次
  • 对比 轻量级锁和 重量级锁还是 优化比较大的

8 docker 镜像

如果有需要, 上面

  • docker pull registry.cn-hangzhou.aliyuncs.com/kaiattrib/test:ubuntu16_glibc23
  • 带 jdk
  • 带 glibc23 源码,可编译
  • 带 自己写的一个jni调用库