我最近对Java的多线程和静态变量产生了一些疑问

package violet.evergarden.yiyi.test;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;

/**

  • 如何在只改变数字的情况下 停止这个线程
  • 条件 : window 4核处理 酷睿i7
  • @author ace
  • @create 2017/11/29.
  • /
    @Slf4j
    public class AuthClientRunner {
private static boolean retry = true;

private static synchronized boolean isRetry() {
    return retry;
}

private static synchronized void setRetry(boolean retry) {
    AuthClientRunner.retry = retry;
}

public static void main(String[] args) {
    for (int i = 0; i < 4; i++) {
        CompletableFuture.runAsync(() -> {
            refreshUserPubKeyS();
            try {
                Thread.sleep(12000);
                setRetry(false);
                log.info("现在变量改为false,并且获取改变后的变量{},现在睡眠的线程是{}",isRetry(),Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, Executors.newFixedThreadPool(1));
    }

}

public static void refreshUserPubKeyS() {
    CompletableFuture.runAsync(() -> {
        while (isRetry()) {
            if (isRetry()) {
                log.error("初始化失败,1分钟后自动重试!");
                try {
                    // 这个不注释就会死循环
                    setRetry(true);
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    log.error("线程睡眠失败,{}", e);
                }

                log.info("我要重新改为true{},现在睡眠的线程是{}",isRetry(),Thread.currentThread().getName());

            }
            if (!isRetry()) {
                setRetry(false);
            }
        }
    }, Executors.newFixedThreadPool(1));
}

}

你这每次循环建2个线程池,每个线程池建一个线程