Asynchronous task pattern in Java
As I moved from c# to Java, I needed to implement a set of asynchronous tasks
I have a good understanding of Java threads, but I like it Net, because it allows me to easily switch tasks from synchronous to asynchronous
In my case, if I have a set of I / O-Intensive operations (suitable for becoming asynchronous), it is as follows:
DoOperation1(); DoOperation2(); DoOperation3();
Yes Net, I can easily do the following:
BeginInvoke(DoOperation1); BeginInvoke(DoOperation2); BeginInvoke(DoOperation3); EndInvoke(Result1); EndInvoke(Result2); EndInvoke(Result3);
In short, my question is: is there anything similar in Java, or do I need to manually use threads "old way"?
thank you.
Solution
You may want to use futures in Java You submit a task to executorservice and receive future < T >, you can query the view in a way similar to "task" From Net 4 TPL... You can ask future results and whether it has been completed by blocking or timeout
Using callable < T > is not as neat as using proxy in C # through method group transformation and lambda expression, but the basic idea is similar