Preface:

In a month’s time is gold, silver and four, I believe that many friends have done enough preparation to deal with this spring recruitment, xiaobian for you also do not have too much advice and help, share some of your interview above can be used to get some interview questions. Today mainly to share with you multithreading (thread safety) aspects of the interview questions, see the article partners must see behind oh, there are a large number of interview questions to share with you yo.

What is thread safety? Are servlets thread-safe?

Thread safety means that multiple threads do not generate abnormal data during execution.

Accessing the same code in multiple threads will not produce uncertain results

Thread safety first of all his is multithreading, at least two. Because a thread is certainly thread-safe. You’re running on your own and nobody’s giving you resources to compete for. You’re executing in serial, there’s no uncertainty. If you have more than one thread that can be a problem. An ArrayList class, for example, might have two steps to add an element:

1. Store this element in the Items[Size] location;

2. Increase the Size value.

Thread A completes the first step and is paused by the CPU. Thread B doesn’t know what the Size value is, so it goes back to where thread A put the element, and it puts the element again, and then Size+1. And then A gets woken up and goes to Size plus 1 and Size is equal to 2, but there’s only one element. I put it in the same place twice and covered it.

How to solve this problem? Think about it

1. Set the operation to atomic operation so that I can execute without being disturbed by others.

2. Let the modified value be notified to other threads in real time.

And I continue to think ~

Servlets are thread-unsafe.

2. How many ways can synchronization be implemented?

The synchronization methods are synchronized wait and notify

3. Please explain the Java memory model and its workflow.

The Java memory model is that Java memory is divided into primary memory and working memory.

Multithreading Each thread has its own working memory, and the variables used are a copy of the main memory.

4. What is volatile for? Can you explain in one sentence how volatile is used?

The effect of Volatile is that when multiple threads use the same variable, main memory reads it each time. The real-time of data is guaranteed. The application scenario is that multiple threads operate on the same variable.

5, how to ensure the visibility of variables in multi-threaded execution?

Visibility: When one thread changes a variable, other threads can immediately know about it

Ways to ensure visibility

volatile

Synchronized (before unlock, write variable value back to main memory)

Final (once initialization is complete, other threads are visible)

6. Why is code reordered?

Code reordering is for efficient CPU execution.

Basic principles for JVM instruction rearrangement

  • Program sequence principle: guarantee semantic serialization within a thread
  • Volatile rule: Writes to volatile variables occur first before reads
  • Lock rule: An unlock must occur before a subsequent lock
  • Transitivity: IF A precedes B and B precedes C, then A must precede C
  • A thread’s start method precedes each of its actions
  • All operations on a Thread precede the termination of the Thread (thread.join ())
  • The code of a thread whose interrupt() precedes the interrupted thread
  • Object constructors terminate before finalize() methods

A mind map for JAVA Concurrency




Recommended reading:

What do you like to ask about Kafka and BAT?

28 ZooKeeper: How to nail an interviewer!

【 Spring recruitment series 】 the latest Linux interview real questions 45

【 Preparation for Spring Recruitment series 】 Over the years about MySQL high frequency interview questions

【 Spring Recruitment series 】springBoot soul 22 ask!

【 Prepare for spring recruitment series 】50 micro service interview questions in detail

【 Prepare for spring recruitment series 】27 MyBatis interview real questions detailed explanation

Q: How do you nail an interviewer

For more technical articles and interview questions, you can follow the wechat official account: Java Programmers Gathering Place.