Introduce

Source:Java Backend Developer Interview Questions (Pt. 1–10) | by DN Tech | Geek Culture | Medium

Start

I don't know why I look at this English article and have the feeling of reading Chinese article.

Source:Java Backend Developer Interview Questions (Pt. 1–10) | by DN Tech | Geek Culture | Medium

This article refers to another English blog, such as the diagram about the role of each area of the JVM from the corresponding article:

[[The JVM Architecture Explained]](Obsidian Two-way linking)

Original reference article: The JVM Architecture Explained - DZone Java

Fabulous

Never! NEVER ask these questions if you need good Java developer in your teem :))

Strongly agree!

1. What is the role of JVM?

Every Java developer knows that bytecode will be executed by the JRE (Java Runtime Environment). But many don't know the fact that JRE is the implementation of Java Virtual Machine (JVM)

JVM is short for Java Virtual Machine. It provides a runtime environment for Java code to run in. It makes sure that Java is compiled once and runs everywhere regardless of operating systems and processors.

2. How is the compilation process of Java source code?

Java source code undergoes source code development

  1. lexical analyzer
  2. syntax analyzer
  3. sematic analyzer
  4. bytecode generator.

3. What is the structure of JVM?

JVM consists of mainly three subsystems:

  • Class Loader subsystem
  • Runtime Data area
  • Execution Engine

How is the compilation process of Java source code?

Image from:https://dzone.com/articles/jvm-architecture-explained

4. In JVM Runtime Data Area, what are the components that are shared between multiple threads?(Important)

Method area is shared among threads.

Heap area stores global variables, object instances, and things that could be accessed anywhere within the applications, Heap area also a shared space.

The stack area which stores the methods, local variables within a thread is not shared as each thread has different methods of calling and variables

5. What are the communication methods between threads in Java?

1) Shared memory

volatile keyword, synchronized keyword

2) wait and notify mechanism

wait() and notify() are the methods of Java Object

wait() method makes object convert from running state to blocking state

another thread will call the notify() method to wake up the first thread, and let it enter a runnable state.

3) Lock/Condition mechanism

Lock is class provided by Java to limit access to an object,Condition is invoked by Lock class.

One lock can create multiple conditions. Using Condition.await() and Condition.signal() methods

6. What are volatile and synchronized keywords?

Volatile keyword targets at a field, or a variable in a method, such that when multiple threads access the same variable, data consistency is ensured.

This is done by forcing the variable to be accessed through main memory instead of cache memory.

Synchronized keyword is used for a block of code. It allows only one thread to access the resources at a given point of time.

When one thread is manipulating the resources, other threads who want to access the same object are not allowed to execute.

7. What are the differences between volatile and synchronized?

Volatile is a lightweight lock whereas synchronized is a heavyweight lock. After synchronized block is writing/modifying the resource, the variable value is flushed back to the shared memory space.

In contrast, volatile allows concurrent executions from multiple threads by forcing them to read the variable from main memory directly instead of CPU cache.

Moreover, synchronized is implemented based on operating system which causes the thread to fall into the kernel mode instead of user mode

8. What is the use of ThreadLocal?

ThreadLocal is a mechanism to ensure thread safety. It allows developer to store variables pertaining to a particular thread so that multiple threads do not need to access shared variables.

Each _Thread_ class has a field called _threadLocals_ of type _ThreadLocal.ThreadLocalMap_, The key for thread locals is the reference to the current ThreadLocal and the value is the variable developer wants to store (of class type T).

To write to or read from the variable, ThreadLocal.get() or set() method are called.

9. What is reflection in Java

Reflection in Java gives the program ability to introspect itself,  It allows developer to examine or modify the behavior of methods, classes or interfaces at runtime.

10. Why is HashMap thread-unsafe?

1) Multiple puts from multiple threads may cause the loss of the element
2) When put and get execute concurrently, the return value for get may be null. This happens when a thread puts an element which exceeds threshold, leading to a rehash operation, a get method will lead to null value.
3) Concurrent put in jdk1.7 may lead to circular linked list which causes infinite loop in get.

The first two I can still understand, but the third I'm a bit of a knowledge blind spot, individuals have not seen the JDK1.7 HashMap source code, using this article for reference

If you want to know the third case What mean is, I recommend the english article:

The Mailinator(tm) Blog: A Beautiful Race Condition

If you English is not so good, You also can see the article

疫苗:Java HashMap的死循环 | 酷 壳 - CoolShell


Xander
198 声望51 粉丝