多线程:只要有一个线程结束,就取消其余线程

package com.wangzs.offer;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 多线程: 只要有一个处理有结果,就立刻结束其他还在运行中的处理方式
 */
@Slf4j
public class TaskCallable implements Callable {
    // 启动线程数量
    private static final Integer THREAD_NUMS = 10;

    // 线程名字
    String name;

    public TaskCallable(String name, int sleep) {
        this.name = name;
    }

    @Override
    public String call() throws Exception {
        int random = new Random().nextInt(10);
        log.info("[{}]任务开始睡眠[{}]秒", name, random);
        Thread.sleep(random * 1000);
        return name;
    }

    public static void main(String[] args) throws Exception {
        ExecutorService pool = Executors.newFixedThreadPool(50); // 线程池大小
        List> results = new ArrayList<>();
        for (int i = 0; i < THREAD_NUMS; i++) { // 启动线程数量
            results.add(pool.submit(new TaskCallable("the " + i, i)));
        }

        log.info("开始等待处理结果");
        boolean breakAllThread = false;
        while (true) {
            for (Future f : results) {
                if (f.isDone()) {
                    log.info("处理已经有了结果{}", f.get());
                    breakAllThread = true;
                    break;
                }
            }

            if (breakAllThread) { // 取消所有线程
                for (Future f : results) {
                    log.info("取消其他线程.");
                    f.cancel(true);
                }
                break;
            }
        }

        pool.shutdown();
    }
}
D:\MyProgram\Java\jdk1.8.0_341\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.2.3\lib\idea_rt.jar=59910:C:\Program Files\JetBrains\IntelliJ IDEA 2022.2.3\bin" -Dfile.encoding=UTF-8 -classpath D:\MyProgram\Java\jdk1.8.0_341\jre\lib\charsets.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\deploy.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\access-bridge-64.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\cldrdata.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\dnsns.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\jaccess.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\jfxrt.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\localedata.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\nashorn.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\sunec.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\sunjce_provider.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\sunmscapi.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\sunpkcs11.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\ext\zipfs.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\javaws.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\jce.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\jfr.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\jfxswt.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\jsse.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\management-agent.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\plugin.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\resources.jar;D:\MyProgram\Java\jdk1.8.0_341\jre\lib\rt.jar;D:\my-code-2023\learn-2023\target\classes;D:\repo_2023\org\springframework\boot\spring-boot-starter\2.6.11\spring-boot-starter-2.6.11.jar;D:\repo_2023\org\springframework\boot\spring-boot\2.6.11\spring-boot-2.6.11.jar;D:\repo_2023\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;D:\repo_2023\org\springframework\boot\spring-boot-autoconfigure\2.6.11\spring-boot-autoconfigure-2.6.11.jar;D:\repo_2023\org\springframework\boot\spring-boot-starter-logging\2.6.11\spring-boot-starter-logging-2.6.11.jar;D:\repo_2023\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\repo_2023\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\repo_2023\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\repo_2023\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\repo_2023\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\repo_2023\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\repo_2023\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;D:\repo_2023\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar;D:\repo_2023\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;D:\repo_2023\org\springframework\boot\spring-boot-starter-test\2.6.11\spring-boot-starter-test-2.6.11.jar;D:\repo_2023\org\springframework\boot\spring-boot-test\2.6.11\spring-boot-test-2.6.11.jar;D:\repo_2023\org\springframework\boot\spring-boot-test-autoconfigure\2.6.11\spring-boot-test-autoconfigure-2.6.11.jar;D:\repo_2023\com\jayway\jsonpath\json-path\2.6.0\json-path-2.6.0.jar;D:\repo_2023\net\minidev\json-smart\2.4.8\json-smart-2.4.8.jar;D:\repo_2023\net\minidev\accessors-smart\2.4.8\accessors-smart-2.4.8.jar;D:\repo_2023\org\ow2\asm\asm\9.1\asm-9.1.jar;D:\repo_2023\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\repo_2023\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;D:\repo_2023\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;D:\repo_2023\org\assertj\assertj-core\3.21.0\assertj-core-3.21.0.jar;D:\repo_2023\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;D:\repo_2023\org\junit\jupiter\junit-jupiter\5.8.2\junit-jupiter-5.8.2.jar;D:\repo_2023\org\junit\jupiter\junit-jupiter-api\5.8.2\junit-jupiter-api-5.8.2.jar;D:\repo_2023\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\repo_2023\org\junit\platform\junit-platform-commons\1.8.2\junit-platform-commons-1.8.2.jar;D:\repo_2023\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;D:\repo_2023\org\junit\jupiter\junit-jupiter-params\5.8.2\junit-jupiter-params-5.8.2.jar;D:\repo_2023\org\junit\jupiter\junit-jupiter-engine\5.8.2\junit-jupiter-engine-5.8.2.jar;D:\repo_2023\org\junit\platform\junit-platform-engine\1.8.2\junit-platform-engine-1.8.2.jar;D:\repo_2023\org\mockito\mockito-core\4.0.0\mockito-core-4.0.0.jar;D:\repo_2023\net\bytebuddy\byte-buddy\1.11.22\byte-buddy-1.11.22.jar;D:\repo_2023\net\bytebuddy\byte-buddy-agent\1.11.22\byte-buddy-agent-1.11.22.jar;D:\repo_2023\org\objenesis\objenesis\3.2\objenesis-3.2.jar;D:\repo_2023\org\mockito\mockito-junit-jupiter\4.0.0\mockito-junit-jupiter-4.0.0.jar;D:\repo_2023\org\skyscreamer\jsonassert\1.5.1\jsonassert-1.5.1.jar;D:\repo_2023\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\repo_2023\org\springframework\spring-test\5.3.22\spring-test-5.3.22.jar;D:\repo_2023\org\xmlunit\xmlunit-core\2.8.4\xmlunit-core-2.8.4.jar;D:\repo_2023\org\springframework\boot\spring-boot-starter-web\2.6.11\spring-boot-starter-web-2.6.11.jar;D:\repo_2023\org\springframework\boot\spring-boot-starter-json\2.6.11\spring-boot-starter-json-2.6.11.jar;D:\repo_2023\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;D:\repo_2023\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;D:\repo_2023\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;D:\repo_2023\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;D:\repo_2023\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;D:\repo_2023\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;D:\repo_2023\org\springframework\boot\spring-boot-starter-tomcat\2.6.11\spring-boot-starter-tomcat-2.6.11.jar;D:\repo_2023\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;D:\repo_2023\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;D:\repo_2023\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;D:\repo_2023\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;D:\repo_2023\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;D:\repo_2023\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;D:\repo_2023\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;D:\repo_2023\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;D:\repo_2023\cn\hutool\hutool-all\5.8.14\hutool-all-5.8.14.jar com.wangzs.offer.TaskCallable
09:02:05.085 [pool-1-thread-4] INFO com.wangzs.offer.TaskCallable - [the 3]任务开始睡眠[5]秒
09:02:05.085 [pool-1-thread-5] INFO com.wangzs.offer.TaskCallable - [the 4]任务开始睡眠[1]秒
09:02:05.085 [pool-1-thread-1] INFO com.wangzs.offer.TaskCallable - [the 0]任务开始睡眠[5]秒
09:02:05.085 [pool-1-thread-7] INFO com.wangzs.offer.TaskCallable - [the 6]任务开始睡眠[3]秒
09:02:05.085 [pool-1-thread-2] INFO com.wangzs.offer.TaskCallable - [the 1]任务开始睡眠[7]秒
09:02:05.085 [pool-1-thread-9] INFO com.wangzs.offer.TaskCallable - [the 8]任务开始睡眠[2]秒
09:02:05.085 [pool-1-thread-8] INFO com.wangzs.offer.TaskCallable - [the 7]任务开始睡眠[2]秒
09:02:05.085 [pool-1-thread-10] INFO com.wangzs.offer.TaskCallable - [the 9]任务开始睡眠[4]秒
09:02:05.085 [pool-1-thread-6] INFO com.wangzs.offer.TaskCallable - [the 5]任务开始睡眠[8]秒
09:02:05.085 [main] INFO com.wangzs.offer.TaskCallable - 开始等待处理结果
09:02:05.085 [pool-1-thread-3] INFO com.wangzs.offer.TaskCallable - [the 2]任务开始睡眠[9]秒
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 处理已经有了结果the 4
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.
09:02:06.097 [main] INFO com.wangzs.offer.TaskCallable - 取消其他线程.

Process finished with exit code 0

你可能感兴趣的:(java)