Runnable is a standalone task that performs work, but it does not return any values. The callable introduced in Java SE5 is a generic type parameter whose type parameter table is the value returned from method call () and must be called with the Executorservices.submit () method, and here is a simple example.
[Java]View Plaincopy print?
Packagecom.test; 2. 3.Importjava.util.ArrayList; 4.Importjava.util.List; 5.Importjava.util.concurrent.Callable; 6.Importjava.util.concurrent.ExecutionException; 7.ImportJava.util.concurrent.ExecutorService; 8.Importjava.util.concurrent.Executors; 9.Importjava.util.concurrent.Future; 10. 11. Public classCallabletest {12. Public Static voidMain (string[] args) {Executorservice exec=Executors.newcachedthreadpool (); List<future<string>> results=NewArraylist<future<string>>(); 15. 16. for(inti=0;i<5;i++) { Results.add (Exec.submit (NewTaskwithresult (i))); 18. } 19. 20. for(future<string>fs:results) { 21st.Try { 22. System.out.println (Fs.get ()); 23.}Catch(interruptedexception e) {24. E.printstacktrace (); 25.}Catch(executionexception e) {26. E.printstacktrace (); 27. } 28. } 29. } 30.} 31. 32.classTaskwithresultImplementsCallable<string> { 33.Private intID; 34. PublicTaskwithresult (intID) {35. This. id=ID; 36. } 37. 38. @Override39. PublicString Call ()throwsException {40.return"Result of Taskwithresult" +ID; 41. } 42.} Packagecom.test;Importjava.util.ArrayList;Importjava.util.List;Importjava.util.concurrent.Callable;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;Importjava.util.concurrent.Future; Public classCallabletest { Public Static voidMain (string[] args) {Executorservice exec=Executors.newcachedthreadpool (); List<Future<String>> results=NewArraylist<future<string>>(); for(inti=0;i<5;i++) {Results.add (Exec.submit (NewTaskwithresult (i))); } for(future<string>fs:results) { Try{System.out.println (Fs.get ()); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(executionexception e) {e.printstacktrace (); } } }}classTaskwithresultImplementsCallable<string> { Private intID; PublicTaskwithresult (intID) { This. id=ID; } @Override PublicString Call ()throwsException {return"Result of Taskwithresult" +ID; }}
Experimental results:
Result of Taskwithresult 0 result of Taskwithresult 1 result of Taskwithresult 2 result of Taskwithresult 3 result of Task Withresult 4
The Submit () method returns the future object, which is parameterized with the specific type of callable return result. You can use the Isdone () method to query whether the future is complete, and when the task is complete, it has a result that can be called by The Get () method to get the result. You can also call get () directly without Isdone (), in which case get () will block until the result is ready. You can also call get () with a timeout before attempting to invoke get (), or call Isdone () to see if the task is complete.
How to use multithreading callable