我不明白'a'的值是0,为什么'a'不是10,那段代码的运行过程是什么,有必要从Java Memory Model分析吗?这是我的测试代码
package com.study.concurrent.demo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
//@SpringBootTest
@Slf4j
class DemoApplicationTests {
int a = 0;
int b = 0;
@Test
void contextLoads() {
ExecutorService executorService = Executors.newFixedThreadPool(1);
// final Semaphore semaphore = new Semaphore(3);
for (int i = 0; i < 10; i++) {
executorService.execute(() -> {
// try {
// semaphore.acquire();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
add();
bdd();
// log.info("a: {},concurrent_id: {}",a,Thread.currentThread().getName());
// semaphore.release();
}); …Run Code Online (Sandbox Code Playgroud)