A bean developer writes a stateless session bean FooEJB with the following asynchronous
business method:
@Asynchronous
public Future<Integer> fooAsync () {
System.out.printIn (begin);
int i = 1;
System.out.print(end);
Return new AsyncResult<Integer> (i);
}
Given the following code, where fooRef is an EJB reference to FooEJB:
Future<Integer> fooFuture = fooref.fooAsync();
fooFuture.cancel (true);
Which two represents possible system output after all processing has completed? (Choose two)
A.
Begin end
B.
Begin
C.
End
D.
1
E.
<no output>
Explanation:
Either it will run and return 1, or it will be cancelled and produce no output.
Note:EJB 3.1 can support a return type of java.util.concurrent.Future<V>, where V represents theresultant value of an asynchronous invocation. In case you are unfamiliar with it, the Future<V>
interface allows you to do things like cancelling an asynchronous invocation, checking if an
invocation is complete, check for exceptions and getting the results of an asynchronous
invocation.
ae
A
A,B
The execution either will start then cancled “so it’ll print begin” or it can be executed faster than it can be canceled, so it’ll print begin Eng
B, E
Since first print is a println and next is print and so we dont have something like that in option A and hence it should be B.
client invokes cancel mathod with parameter mayInterruptIfRunning = true, so there are two possible answers:
1) Begin End – if asynchronous method has been ended before cancel invocation
2) Begin – if asynchronous method has been canceled during its execution
So A and B are correct.
AE
A Async method cannot be interrupted in the middle, or is fully executed or not.
I think it is AE, because according to this Stackoverflow question, the future can only be interrupted if the future method itself is checking if it was interrupted or not:
http://stackoverflow.com/questions/9536555/utility-of-future-cancelboolean-method
DE is correct one !