preface

When interviewing Java, you will inevitably be asked about the Java memory model and concurrent development in Java. When I was asked, I was like, “Well, is that in Thinking in Java? Sure enough, it’s too low to add, delete and change every day.”

Do you want to know these pictures?

I wish I could explain it in a simpler way, but none of the above

Java concurrent code
public class Example1 {
    public static int count = 0;
    public static int clientTotal = 5000;
    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < clientTotal ; i++) {
            executorService.execute(() -> {
                try {
                    add();
                } catch (Exception e) {
                    log.error("exception", e); }}); } } private static voidadd() { count++; }}Copy the code

What is the value of count if the above code executes? (To illustrate the point, the last printed code was not written) 5000? As a result of multiple runs, count is less than 5000.

To explain the above program, we first define a thread pool and start 5000 threads to perform add(). The add function handles the static member variable count.

If the program is called sequentially, the value of count should be 5000.

for(int i=0; i<5000; i++){ add(); }Copy the code

But thread pools start multithreading and execute concurrently. After each thread is started, the next thread is started immediately, whether it finishes running or not.

The process of starting a thread, which is an asynchronous process, immediately returns to start the next process.

Write conflicts occur when multiple threads operate on the same variable add.

Thread 1 and thread 2 operate on a variable with a value of 0 at the same time, and return 1 instead of 2. If you can’t figure this out, leave a comment or take a look at the schematics at the top of this article.

To keep things simple, remember that “multithreading writes to global variables can conflict.”

Declare the atomic variable AtomicInteger count

Public class CountExample2 {public static int clientTotal = 5000; Public static int threadTotal = 200; public static AtomicInteger count = new AtomicInteger(0); public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(threadTotal); final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);for (int i = 0; i < clientTotal ; i++) {
            executorService.execute(() -> {
                try {
                    semaphore.acquire();
                    add();
                    semaphore.release();
                } catch (Exception e) {
                    log.error("exception", e);
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("count:{}", count.get());
    }
    private static void add() { count.incrementAndGet(); // count.getAndIncrement(); }}Copy the code

Note, the above code uses the generator consumer model, 5000 producers, 200 consumers, to limit the concurrency of the program, to prevent 5000 threads from blocking the computer.

Memory model, let’s do something simple

    1. Stack, “heap,” the space allocated for variables inside a function when it is loaded. Share the same area as the parent function’s internal variables and run Pointers.
    1. When the function runs, the space for new is all in the heap.

So this is the memory model of C, the basics of shellcode.

Author: white leading wild goose link: www.jianshu.com/p/8cb84ce74…

To read more

Finally, I made a map of China

SVG foreplay – Make your View colorful

Share a few of Android’s most powerful open source frameworks

(Android) Interview Questions