Recently, I interviewed a lot of candidates, and found that many of them fail to answer questions about concurrency. For example:

  • Me: What problems do you use volatile to solve in your work?
    • Candidate:
  • Me: What’s the difference between volatile and synchronized? What are the scenarios for each of them?
    • Candidate:
  • Me: Do you understand how volatile implements visibility and order?
    • Candidate:
All right, then there’s no then
A lot of candidates may wonder why the interviewer always asks me to write something about it. I don’t need CRUD in my daily work

So let’s see if concurrent programming is really so important, and why do interviewers always ask?? Here’s a screenshot I found in the tech group:

Therefore, learning concurrent programming is essential for achieving promotion and salary increase through interview, project optimization and concurrent promotion, and love technology and want to study framework source code!!

So how do you learn it? Here I sort out the learning path, how to smoothly master a complete set of knowledge system, hoping to give you some reference:

So in this article, we’ll start with volatile. If you want to learn more about basic multithreading, check out the qr code at the bottom of this article and follow my public account for updates in the near future.

Not much. Get to the point. Take a look at the following code and think about the result
public class VolatileTest {
    public volatile int race = 0;
    public void increase(a) {
        race++;
    }
    public  int getRace(a){
        return race;
    }

    public static void main(String[] args) {
        // Create five threads and add the same volatileTest instance
        VolatileTest volatileTest=new VolatileTest();
        int threadCount = 5;
        Thread[] threads = new Thread[threadCount];/ / 5 threads
        for (int i = 0; i < threadCount; i++) {
            // Each thread performs 10000 ++ operations
            threads[i]  = new Thread(()->{
                for (int j = 0; j < 10000; j++) {
                    volatileTest.increase();
                }
                System.out.println(Thread.currentThread().getName()+"After 10,000 ++ runs, the race value is:"+volatileTest.getRace());
            },"Thread"+(i+1));
            threads[i].start();
        }

        // Wait for all accumulated threads to finish
        for (int i = 0; i < threadCount; i++) {
            try {
                threads[i].join();
            } catch(InterruptedException e) { e.printStackTrace(); }}// After all child threads have finished, the theoretical value of race should be: 5*10000=50000, but the result of execution is less than that. Why?
        System.out.println("Sum result:"+volatileTest.getRace()); }}Copy the code
In VolatileTest, race is an int variable with volatile and its initial value is 0. Five threads are created to add the same volatileTest instance. Each thread performs the ++ operation for 10000 times. Thus, the theoretical value of race should be: 5*10000=50000 after all child threads are finished, but the result of execution will be less than it, as shown in the figure below.

Heart is not collapse, an i++ will give a problem??
This is a bad use of volatile. To understand why, consider the following:
  • The three main features of concurrency, and what are the main features of Volatile?
  • To understand the implementation principle of i++, you need to master the knowledge of bytecode instructions

This article will give you an idea of how to learn concurrent programming. It will show you a volatile error, why it went wrong, and what to learn about itFigure out i++ and surprise your interviewerIn the detailed answer oh

If you find this article helpful, please give it a thumbs up

The editor will continue to bring you hot technical articles, let’s progress together