The difference between runnable and callable is that
(1) The method prescribed by callable is call (), and the method specified by runnable is run ().
(2) The callable task can return a value after execution, and Runnable's task is not to return a value.
(3) Call method can throw an exception, the Run method can not.
(4) Run callable task can get a future object,
The future represents the result of an asynchronous calculation. It provides a way to check whether the calculation is complete, to wait for the calculation to complete, and to get the results of the calculation. You can only use the Get method to get the results after the calculation is complete.
The Future.get () method may block execution of the current thread if the thread is not finished executing;
If there is an exception to the thread, Future.get () throws Interruptedexception or executionexception;
If the thread has been canceled, it will run out of Cancellationexception. Cancel is performed by the Cancel method.
Isdone determines whether the task is completed normally or canceled. Once the calculation is complete, you can no longer cancel the calculation.
If you use the future for the sake of cancellation and do not provide the available results, you can declare the Future<?> form type and return null as the result of the underlying task.
The future interface is defined as follows:
Future mode
In the future mode, when a request occurs, a future credential is generated to the client requesting it, which acts like a proxy object, while a new thread of execution continues to generate the target object (Thread-per-message), and after the actual target object is generated, Set it to the future, and when the client really needs the target object, the target object is ready for the customer to use.
In combination with the future of the JDK, after you run the thread, you can assign the return value of the thread to the future and return a future object. At this point you can get the object immediately and then proceed to the following logic. But if you want to get the result of the thread in the future, it will be blocked until the thread ends.
The equivalent of the present, you put the formalities and money are handed up, you can get the contract immediately, but only the contract no house. This time you have a family, you can buy home appliances to buy the decoration (go to the following logic). But you have to put the appliance and the decoration in, you must wait until the house is finished (blocked).
Java callable,runnable Comparison and usage