Java 8 Completable Future - 并行执行

Java 8 Completable Future - parallel execution

我正在测试 CompletableFuture 的工作原理。我对如何并行执行任务很感兴趣:

try {
          CompletableFuture one = CompletableFuture.runAsync(() -> {
          throw new RuntimeException("error");
          });
          CompletableFuture two = CompletableFuture.runAsync(() -> System.out.println("2"));
          CompletableFuture three = CompletableFuture.runAsync(() -> System.out.println("3"));
          CompletableFuture all = CompletableFuture.allOf(one, two, three);
          all.get();
} catch (InterruptedException e) {
           System.out.println(e);
} catch (ExecutionException e) {
           System.out.println(e);
}

在这种情况下,他们将被全部执行。

1。当其中一个线程出现异常时,是否可以中断所有 运行 个线程?

2。当此代码位于可以从不同线程调用的 class' 方法中时,它是线程安全的吗?

1.Is it possible to interrupt all running threads when there is an exception in one of them?

是的,这是可能的。所有线程都应该可以访问公共对象,该对象的状态可以被其他线程更改和读取。例如,它可以是 AtomicInteger。请参阅以下示例:

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

public class Dates {

    public static void main(String[] args) throws Exception {
        try {
            AtomicInteger excCounter = new AtomicInteger(0);
            CompletableFuture one = CompletableFuture.runAsync(new ExcRunnable(excCounter));
            CompletableFuture two = CompletableFuture.runAsync(new PrintRunnable("2", excCounter));
            CompletableFuture three = CompletableFuture.runAsync(new PrintRunnable("3", excCounter));
            CompletableFuture all = CompletableFuture.allOf(one, two, three);
            all.get();
        } catch (InterruptedException | ExecutionException e) {
            System.out.println(e);
        }
    }
}

class ExcRunnable implements Runnable {
    private final AtomicInteger excCounter;

    public ExcRunnable(AtomicInteger excCounter) {
        this.excCounter = excCounter;
    }

    @Override
    public void run() {
        Random random = new Random();
        int millis = (int) (random.nextDouble() * 5000);
        System.out.println("Wait " + millis);
        Threads.sleep(450);

        // Inform another threads that exc occurred
        excCounter.incrementAndGet();

        throw new RuntimeException("error");
    }
}

class PrintRunnable implements Runnable {

    private final String name;
    private final AtomicInteger excCounter;

    public PrintRunnable(String name, AtomicInteger excCounter) {
        this.name = name;
        this.excCounter = excCounter;
    }

    @Override
    public void run() {
        int counter = 10;
        while (counter-- > 0 && excCounter.get() == 0) {
            System.out.println(name);
            Threads.sleep(450);
        }
    }
}

class Threads {
    static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

我们有 3 个任务:两个打印它的名称,一个在一段时间后抛出异常。在抛出异常之前,计数器会增加以通知其他任务其中一个任务失败并且它们应该完成执行。打印作业正在检查此计数器,如果不满足条件,它们将完成作业。当您评论 excCounter.incrementAndGet(); 行时,其他任务在不知道其中一个抛出异常的情况下完成了他们的工作。

  1. When this code is inside a class' method which can be invoked from different threads will it be thread safe?

看看 thread safety 的定义。例如,假设打印任务随着打印的每一行递增公共计数器。如果计数器是原始的 int 它不是线程安全的,因为计数器值可以被替换。但是如果你使用AtomicInteger它是线程安全的,因为AtomicInteger是线程安全的。

1

如果您的任何异步任务抛出异常,all.get() 将抛出异常。 这意味着,您可以取消 catch 子句中的所有 CF。 但是,您的异步任务需要对中断友好,即定期检查中断标志或尽早处理 InterruptedException 和 return。

Task cancellation should always be handled using interrupt mechanism

2

你说的引用变量都是局部的,不用担心线程安全问题。局部变量始终是线程安全的。