Whether java concurrent notes evidencing synchronized lock real

Warning ⚠️: This article takes a long time, to be psychologically prepared
Proof: biased locking, lightweight lock, lock real heavyweight
By the [java java concurrent threading model] notes the link:  https://www.cnblogs.com/yuhangwang/p/11256476.html this article shows: Whenever java threads created corresponding os pthread_create () will create a thread, using synchronized () is bound to call os pthread_mutex_lock () function
synchronized keyword lock status: no lock, tend to lock, lock lightweight, heavyweight lock
This article from the tend to prove the existence of the lock to start, we all know it will be biased locking thread-safe, but the reality is not necessarily mutually exclusive, biased locking the case of synchronized lock object does not have the resources to compete, does not call os pthread_mutex_lock () function; but first initialized using the lock when does one call pthread_mutex_lock () were biased locking
Conjecture: biased locking certain real
Confirmation method:
  1. Modify pthread_mutex_lock () method, to increase the output current os id Linux source code statement glibc library pthread_mutex_lock.c file;
  2.java synchronized keyword lock code used to print the front locking thread id (id this thread will thread into real os Id), and both compared with each other 1 2;
  3. If you call os pthread_mutex_lock () os-id with the same java thread-id: Description lock really exist, and only appeared once for the same biased locking
Start confirmation:
:( environment to build here note that linux kernel must correspond gilbc library or the compiler unsuccessful )
Linux: CentOS 7   
Gilbc: Gilbc 2.19-official gilbc link http://mirror.hust.edu.cn/gnu/glibc/ 
avoid stepping pit, personal environment has uploaded Baidu network disk: download and install your own Baidu network disk address: Link : https: //pan.baidu.com/s/1LULbCoxm-ooPnZbGG3tdAQ password: 9jwu

Check the environment:
1, first check your linux there is no java environment:
    View is available through java, and two command javac
    If not then check yum among them, which can provide JDK installation command:
yum search java | grep -i --color jdk

 If not,  to jdk8 example:

yum install -y java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64  

Second, checking inux environment should correspond to those of gIibc version (which does not correspond causes the compiler to fail)
Third, no check installed gcc, to compile C program, it does not install gcc
yum -y install gcc

Fourth, unpack glibc

tar -zxvf glibc-2.19.tar.gz

Fifth, modify the source code glibc

Review: pthread_mutex_lock () (i.e. when printing call the method) of the process, the threadId added Print OS 
{your} /glibc-2.19/nptl/pthread_mutex_lock.c the dir---- the pthread_mutex_lock (): the path 
   the file pthread_mutex_lock.c the pthread_mutex_lock () function under the line 66 be modified to:
      the printf (stderr, "% MSG = LU TID \ n-", to pthread_self); 
  (stderr: file descriptor, pthread_self: current thread id)

After modifying, all subsequent calls pthread_mutex_lock () function will print out your own thread id
Sixth, the compiler just modify a file:
cd glibc-2.19

  After compilation to be stored in file location:

mkdir out

   Compilation ( after completion override the system default files, the default file backup in advance, in case of emergency )

cd out
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin

  carried out:

the make 
// the make after finished then execute 
make install

   Test run:

java

 Only calls pthread_mutex_lock () to print out your own thread id:

  

Pthread_mutex_lock here represent modify the source code under linux glibc libraries () successfully;
The next step was to verify biased locking in the end is not real existence:
java code:
import java.lang.Thread;

public class ThreadTest {
    Object o= new Object();
    static {
        System.loadLibrary( "TestThreadNative" );
    }
    public static void main(String[] args) {
        //打印出主线程
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        ThreadTest example4Start = new ThreadTest();
        example4Start.start();
    }
    public void start(){
        Thread thread = new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        sync();
                    } catch (InterruptedException e) {

                    }
                }
            }
        };

        Thread thread2 = new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        sync();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        thread.setName("t1");
        thread2.setName("t2");
        
        thread.start();
    }

    //.c will print out java threaid corresponding threadid os
    public native void tid();

    Sync void public () {throws InterruptedException 
        the synchronized (O) { 
            // ThreadID Java thread id is given jvm not really corresponding to os thread id 
            //System.out.println(Thread.currentThread (). getId ()) ; 

            // Get java thread corresponding to print out the real os thread ID 
            TID (); 
        } 
    } 
}

Get real thread id (os thread Id) :( c-file correspondence native void tid top java code ())

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include "Example4Start.h"
JNIEXPORT void JNICALL Java_Example4Start_tid(JNIEnv *env, jobject c1){
    printf("current tid:%lu-----\n",pthread_self());
    usleep(700);
}

Then go again generated .class, .h, so then executed (this method [java java concurrent threading model] notes the link:  https://www.cnblogs.com/yuhangwang/p/11256476.html have corresponding execution method, please refer to)
carried out:
java ThreadTest
display: 

  

  

Real hammer: Red biased locking prove real, biased locking prove synchronized green after 1.6 has been optimized (os function only called once, do not call back )
Similarly lightweight and heavyweight lock lock can prove this out 

 

Guess you like

Origin www.cnblogs.com/yuhangwang/p/11258599.html