Experience sharing: What kind of people do big factory interviewers like to hire? +Java lock technology dry goods sharing

 

Like and follow, you won’t get lost!   

Preface

A friend told me about my work the day before yesterday. After obtaining his consent, I will show it to you in the first person below.

Finally, I asked a programmer for an interview. The programmer has three years of work experience and expects a salary of 10k. I think this job seeker is pretty good in all aspects.
In the programmer industry, with three years of work experience and a first-tier city, people expect low salaries.

So I happily said to the leader, "How about it, this time I should be satisfied."

Who knows the leader said that this job seeker is not familiar with the technical points of multi-threading, thread pool, and attribute animation.

Although he has done a lot of projects in the past three years, most of them are similar. They usually have some additions, deletions, corrections and checking functions. The projects have no characteristics and the core technology of the project is relatively small. The technical content is actually equivalent to a year of work experience.
Not to be hired.

So dizzy, I have to ask someone to come for an interview again.
This position has been recruited for half a month, and still has not been recruited, but the leader came to ask for someone every other time, and the result came and said that it was not appropriate. This requirement is too high.

Tired!

Like our IT workforce, project experience and core skills are more important than working years. I have been a rookie with a crud, and my core skills cannot be improved. Programming is an industry that requires constant updating of the knowledge base. There are benefits at the end of the article to help you collect the latest technology news in the industry

 

Today, I will introduce to you one that can help you solve more than 90% of the lock problems in Java interviews (locks in Java)

Information analysis of java header

First of all, why should I go to study java object headers?
Here is a screenshot of the comment in the source code of hotspot.
Concurrent lock
This picture is replaced by a human-readable table as followsConcurrent programming

It means that the object header of java will have different manifestations in different states of the object. There are mainly three states, unlocked state, locked state, and gc marked state. Then I can understand that the lock acquisition in java can actually be understood to lock the object, that is, to change the state of the object header, and enter the synchronization code block if the lock is successful. However, there are many types of locks in java. From the figure above, it can be seen that they are roughly divided into three lock states: bias lock, light lock, and weight lock. The efficiency of these three types of locks is completely different. The analysis of efficiency will be analyzed below. We can only use the lock reasonably if we design the code reasonably. So what are the principles of these three types of locks? So we need to study this object first.

The layout of the java object and the layout of the object header

1. JOL to analyze the object layout of java

1 首先添加JOL的依赖
 2 <dependency>
  3 <groupId>org.openjdk.jol</groupId> 
  4 <artifactId>jol‐core</artifactId>
5 <version>0.9</version>
 6 </dependency>

 

A.java

1 public class A { 
2 //没有任何字段 
3 }

 

JOLExample1.java

1 package com.luban.layout; 
2 import org.openjdk.jol.info.ClassLayout; 
3 import org.openjdk.jol.vm.VM; 
4 import static java.lang.System.out; 
5
6 public class JOLExample1 {
7 public static void main(String[] args) throws Exception { 
8 out.println(VM.current().details()); 
9 out.println(ClassLayout.parseClass(A.class).toPrintable()); 
10 } 
11 }

 

operation result

1 # Running 64‐bit HotSpot VM. 
2 # Using compressed oop with 0‐bit shift. 
3 # Using compressed klass with 3‐bit shift. 
4 # Objects are 8 bytes aligned. 
5 # Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] 
6 # Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] 
7
8 com.luban.layout.A object internals: 
9 OFFSET SIZE TYPE DESCRIPTION VALUE 
10 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 
11 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
12 8 4 (object header) 82 22 01 20 (10000010 00100010 00000001 00100000) (536945282) 
13 12 4 (loss due to the next object alignment) 
14 Instance size: 16 bytes 
15 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

 

Analysis result 1

1 Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]对应:[Oop(Ordinary Object Pointer), boolean, byte, char, short, int, float, long, double]大小

The entire object is 16B in total, of which the object header (Object header) 12B and 4B are aligned bytes (because the size of the object on a 64-bit virtual machine must be a multiple of 8), because there is no field in this object, the object The instance data is 0B? Two questions

  • 1. What is the instance data of an object?

  • 2. So what exactly is stored in 12B in the object header?

First of all, we must understand what object instance data is very simple. We can add a boolean field to A. Everyone knows that the boolean field occupies 1B, and then look at the result
A.java

1 public class A {
 2 //占一个字节的boolean字段 
 3 boolean flag =false; 
 4 }

 

Operation result 2

1 # Running 64‐bit HotSpot VM. 
2 # Using compressed oop with 0‐bit shift. 
3 # Using compressed klass with 3‐bit shift. 
4 # Objects are 8 bytes aligned. 
5 # Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] 
6 # Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] 
7
8 com.luban.layout.A object internals: 
9 OFFSET SIZE TYPE DESCRIPTION VALUE 
10 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
11 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
12 8 4 (object header) 82 22 01 20 (10000010 00100010 00000001 00100000) (536945282) 
13 12 1 boolean A.flag false 
14 13 3 (loss due to the next object alignment) 
15 Instance size: 16 bytes 
16 Space losses: 0 bytes internal + 3 bytes external = 3 bytes total

 

Analysis result 2

The size of the entire object has not changed, a total of 16B, of which the object header (Object header) 12B, the boolean field flag (object instance data) accounts for 1B, and the remaining 3B is the alignment byte. From this we can think that the layout of an object is roughly divided into three parts: the object header and the byte alignment of the instance data of the object.

Next, we will discuss the second question, why is the object head 12B? What are stored in this 12B? (The length of the VM object header of different digits is different, here refers to the 64-bit vm) Some professional terms about the java object header

http://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html
First quote the explanation of the object header in the openjdk document

object header Common structure at the beginning of every GC-managed heap object. (Every oop points to an object header.) Includes fundamental information about the heap object’s layout, type, GC state, synchronization state, and identity hash code. Consists of two words. In arrays it is immediately followed by a length field. Note that both Java objects and VM- internal objects have a common object header format

The above reference mentioned that a java object header contains 2 words, and it contains the layout, type, GC status, synchronization status, and identification hash code of the heap object. How to include it? Which two words are they?

mark word The first word of every object header. Usually a set of bitfields including synchronization state and identity hash code. May also be a pointer (with characteristic low bit encoding) to synchronization related information. During GC, may contain GC state bits.

Mark word is the first word. According to the document, it can be known that it contains lock information, hashcode, gc information, etc. What is the second word?

klass pointer The second word of every object header. Points to another object (a metaobject) which describes the layout and behavior of the original object. For Java objects, the “klass” contains a C++ style “vtable”.

Klass word is the second word of the object header, which mainly points to the metadata of the object.
Insert picture description here
Suppose we understand that an object header is mainly composed of the above two parts (except for array objects, the object header of an array object also contains an array length), then how big is a java object header? We know from the source code comments of JVM that one mark word is 64bit, so what is the length of klass?

So we need to find a way to get the detailed information of the java object header, verify its size, and verify that the information contained in it is correct.

According to the above-mentioned object header information printed by JOL, it can be known that an object header is 12B, of which 8B is mark word, then the remaining 4B is klass word, and the lock related is mark word, then focus on analyzing the information in mark word

In the case of no lock, the first 56 bits in the markword store the hashcode of the object, so let’s verify

Java code and running results:

1 public static void main(String[] args) throws Exception { 
2 A a= new A(); 
3 out.println("befor hash"); 
4 //没有计算HASHCODE之前的对象头 
5 out.println(ClassLayout.parseInstance(a).toPrintable()); 
6 //JVM 计算的hashcode 
7 out.println("jvm‐‐‐‐‐‐‐‐‐‐‐‐"+Integer.toHexString(a.hashCode())); 
8 HashUtil.countHash(a); 
9 //当计算完hashcode之后,我们可以查看对象头的信息变化 
10 out.println("after hash"); 
11 out.println(ClassLayout.parseInstance(a).toPrintable()); 
12
13 }

 

1 befor hash 
2 com.luban.layout.A object internals: 
3 OFFSET SIZE TYPE DESCRIPTION VALUE 
4 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 
5 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
6 8 4 (object header) 43 c1 00 20 (01000011 11000001 00000000 00100000) (5 36920387) 
7 12 1 boolean A.flag false 
8 13 3 (loss due to the next object alignment) 
9 Instance size: 16 bytes 
10 Space losses: 0 bytes internal + 3 bytes external = 3 bytes total 
11
12 jvm‐‐‐‐‐‐‐‐‐‐‐‐0x6ae40994 
13 util‐‐‐‐‐‐‐‐‐‐‐0x6ae40994 
14 after hash 
15 com.luban.layout.A object internals: 
16 OFFSET SIZE TYPE DESCRIPTION VALUE 
17 0 4 (object header) 01 94 09 e4 (00000001 10010100 00001001 11100100) (‐469134335) 
18 4 4 (object header) 6a 00 00 00 (01101010 00000000 00000000 00000000) (106) 
19 8 4 (object header) 43 c1 00 20 (01000011 11000001 00000000 00100000) (536920387) 
20 12 1 boolean A.flag false 
21 13 3 (loss due to the next object alignment) 
22 Instance size: 16 bytes 
23 Space losses: 0 bytes internal + 3 bytes external = 3 bytes total 
24
25
26 Process finished with exit code 0

 

http://tool.oschina.net/hexconvert/ hex conversion

1 package com.luban.layout; 
2 import sun.misc.Unsafe; 
3 import java.lang.reflect.Field; 
4
5 public class HashUtil { 
6 public static void countHash(Object object) throws NoSuchFieldException, IllegalAccessException { 
7 // 手动计算HashCode 
8 Field field = Unsafe.class.getDeclaredField("theUnsafe"); 
9 field.setAccessible(true); 
10 Unsafe unsafe = (Unsafe) field.get(null); 
11 long hashCode = 0; 
12 for (long index = 7; index > 0; index‐‐) { 
13 // 取Mark Word中的每一个Byte进行计算 
14 hashCode |= (unsafe.getByte(object, index) & 0xFF) << ((index ‐ 1) * 8);
15 } 
16 String code = Long.toHexString(hashCode); 
17 System.out.println("util‐‐‐‐‐‐‐‐‐‐‐0x"+code); 
18
19 } 
20 }

Due to space issues, analysis results 3 and 4 will be updated in the next issue

At last

That's it for today's content, I hope it will be helpful to everyone.

As far as the interview is concerned, we can easily grasp a lot of knowledge, but for the purpose of learning, you will find many things. We have to go deep into the computer to discover the mysteries.

Finally, I want to say something to you. I have worked for so many years and have interviewed some people for others. Whether it is from the perspective of the interviewer or the leader, in addition to interview skills and experience, great technology and project experience are also their trump cards and confidence. Core technology sharing of first-tier manufacturers

 It took me a long time to sort out some learning materials. What I posted above is the tip of the iceberg in the materials. I hope I can help you! Click to learn the secret code together: csdn

                         

  I will share more pure dry goods articles in the follow-up, and hope to really help you. Your support is my biggest motivation! Welcome to follow and like!

                                                       

Guess you like

Origin blog.csdn.net/weixin_50333534/article/details/109265528