, ForkJoinTask.join() , Future.get() ( join() Javadoc, get() ). Future.get() :
, , Future.get() .
, " " Future/FJT. , , FJT, FJT.join(). , , , , , .
, final . , :
public static void main(String... args) throws Exception {
ExecutorService s = Executors.newCachedThreadPool();
Future<MyObject> f = s.submit(() -> new MyObject(42));
assert (f.get().x == 42);
s.shutdown();
}
public class MyObject {
int x;
public MyObject(int x) { this.x = x; }
}
, Stream ( Stream.of.parallel Executor.submit Stream.collect FJT.join/Future.get), thread, , - . , , HB on submit, :
public static void main(String... args) throws Exception {
ExecutorService s = Executors.newCachedThreadPool();
MyObject o = new MyObject(42);
Future<?> f = s.submit(() -> o.x++);
f.get();
assert (o.x == 43);
s.shutdown();
}
public static class MyObject {
int x;
public MyObject(int x) { this.x = x; }
}
( , HB read(o.x) , store(o.x, 43))