-1

I am implementing CompletableFuture as below but getting an error saying

CompletableFuture(Object) has private access in CompletableFuture

public CompletableFuture<A> init(B b) {

    C c = new C();  
    CompletableFuture<A> future = new CompletableFuture<A>(c);

    return future;
}

public class C implements Callable<A> {

    public A call() throws Exception {
        A a = new A();
        return a;
    }
}

I hope solution to overcome this error?

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Malinda
  • 336
  • 2
  • 12
  • 1
    What exactly are you trying to do here? – Tunaki Feb 26 '16 at 11:12
  • 2
    Which part of “CompletableFuture(Object) has private access” don’t you understand? `private` means that constructor is not meant to be used by other classes. This includes your class. Besides the access issue, what makes you think, you can pass a `Callable` to that constructor and get a useful behavior? Are you mixing up `CompletableFuture` and `FutureTask`? – Holger Feb 26 '16 at 11:18
  • @Holger - I have successfully implemented `FutureTask` and now trying to implement `CompletableFuture` because I want to work asynchronously. – Malinda Feb 27 '16 at 06:39
  • What’s stopping you from using `FutureTask` asynchronously? – Holger Feb 28 '16 at 10:04
  • 1
    We do not have control of the future when it completes. But in `ComletableFuture` we can call .complete whenever we need to submit it. Just google about the best suite. – Malinda Feb 28 '16 at 10:27
  • Since you said “I have successfully implemented `FutureTask`”, that doesn’t make any sense. You can control the completion of a `FutureTask`, when you have implemented it yourself. If you haven’t implemented it, contrary to what you said, then the executor will control it, but that’s not different to using `supplyAsync`, where the completion is determined by the framework. – Holger Feb 29 '16 at 13:46
  • 1
    Please check the difference between `FutureTask` and `CompletableFuture`. Then you will see we have better control from `CompletableFuture` when we are working on asynchronous mode. – Malinda Feb 29 '16 at 17:22
  • @Holger `CompletableFuture` will be use full when you have external dependencies to current thread. As example if you are looking for a response from a server to complete task. And also there are some better control over `FutureTask` in `CompletableFuture`. – Malinda Feb 29 '16 at 17:45

1 Answers1

3

There's no public constructor in CompletableFuture class which accepts a parameter. It's better to use CompletableFuture.supplyAsync() static method which takes a Supplier instead of Callable:

CompletableFuture<A> future = CompletableFuture.supplyAsync(A::new);

Or using lambda:

CompletableFuture<A> future = CompletableFuture.supplyAsync(() -> new A());

Note that if A constructor throws checked exception, you should handle it as Supplier interface does not support exceptions:

CompletableFuture<A> future = CompletableFuture.supplyAsync(() -> {
    try { return new A(); }
    catch(Exception ex) { throw new RuntimeException(ex);}
});

Finally note that supplyAsync will not only create the CompletableFuture, but also schedule it for execution in common thread pool. You can specify custom Executor as second parameter to supplyAsync method. If you want to create CompletableFuture without sending it imediately for execution, then probably you don't need a CompletableFuture.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • Thank you for the explanation. I may have miss understood the concept. I have one question further. Is that mean, there is no need of a class which has extend from `Callable`?. In my code that is the point where do a time consuming task. According to your explanation the task should be done in `try catch` block? If then where should I `complete` the future? – Malinda Feb 27 '16 at 06:38
  • @Malinda, yes, in try-catch block. You may create new class (implementing `Supplier`, not `Callable`), but for simple functions it's easier to write lambda instead. To complete the future, just return the value from the lambda (or `Supplier.get` method in case if you create new class). – Tagir Valeev Feb 27 '16 at 10:38
  • I implemented this successfully. Thank you for the help. – Malinda Feb 27 '16 at 11:42