Summarized a few interview questions about Java locks to see if you can master them

Get technical dry goods and industry information for the first time!

Summarized a few interview questions about Java locks to see if you can master them

The biggest difference between a paid group and a non-charged group is not in the advertisement itself, but in whether it is worth the time to post an advertisement.

Yesterday, I was discussing issues with several netizens in the group. Some people were worried that if math is not good, it is really impossible to learn programming. This is completely wrong, but if you can't learn mathematics well, it will be difficult to develop deeper. Mathematics and programming are closely linked, but many people are used to API calls and can't feel it anymore. For example, base conversion, sorting, etc. are the embodiment of mathematical knowledge and the application of mathematical knowledge. This is usually achieved through API calls. You only stay on the surface, so you may not experience the existence of mathematics in programming.

Summarized a few interview questions about Java locks to see if you can master them
Today, I will discuss a few interview questions about Java locks for you to see if you can be embarrassed!

1. There is a method a, the internal code is as follows:

Summarized a few interview questions about Java locks to see if you can master them

Will the above code be reordered?

In response to this question, many people answered that code 1 and code 2 will be reordered, code 3 will not be reordered, and code 4 and code 5 will be reordered. It is also said that reordering between codes 3, 4, and 5 will also occur. In fact, they are all wrong, because the question itself is wrong. Volatile cannot be used in method code. Volitale is a member variable modifier.

2. How to ensure the visibility of a shared variable?

There are many ways to do this. Solution 1. To ensure the visibility of shared variables, use the volatile keyword to modify. Solution 2: Ensure that shared variables are private, use set/get methods to access variables, and use synchronized to lock methods. This method not only ensures visibility, but also ensures thread safety. Solution three, use atomic variables, for example, AtomicInteger, etc.

3. Will the thread safety issue occur on a 32-bit machine for the long type?

will happen. Because long is a 64-bit data type. Write operations performed on a 32-bit CPU will be split into two write operations. Therefore, thread safety issues may occur.

4. Is the following code thread safe?

Summarized a few interview questions about Java locks to see if you can master them

Not thread-safe. Cannot use a and b as lock objects. The two objects a is an Integer and b is a String. They are both immutable objects. Once they are assigned, they will become new objects, and the added lock will become invalid.

I have been very busy recently, and continue to share dry goods later. Also, if you want to see the knowledge points in that area, you can leave a comment and share it later.

Guess you like

Origin blog.51cto.com/15127565/2666116