package com.example.demo;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
public class VolitileTest {
static AtomicInteger num = new AtomicInteger(0);
static CountDownLatch countDownLatch = new CountDownLatch(30);
public static void main(String[] args) {
for (int i = 0; i < 30; i++) {
new Thread(() -> {
for (int j = 0 ; j< 10 ; j++){
num.getAndIncrement();
}
countDownLatch.countDown();
}).start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(num);
}
}
class b{
static volatile boolean flag = false;
public static void main(String[] args) {
for (int i = 0 ; i < 10 ; i++){
new Thread(()->{
if(flag == true){
System.out.println(Thread.currentThread().getName() + "run");
}else{
System.out.println(Thread.currentThread().getName() + "false");
}
}).start();
}
new Thread(()->{
flag = true;
}).start();
}
}
class c{
}