(1) Inheriting the thread class, overriding the Run function Create:
Class XX extends thread{
public void Run () {
Thread.Sleep (1000)/thread sleeps for 1000 milliseconds, sleep causes the thread to enter the block state, and frees the resource
}}
To open a thread:
Object. Start ()//boot thread, run function
(2) Implement runnable interface and rewrite the run function To open a thread:
Threads t = new Thread (object)//Create threading Object
T.start ()
(3) Implement callable interface, rewrite call function Callable are interfaces similar to runnable, classes that implement callable interfaces, and classes that implement runnable are tasks that can be performed by other threads.
There are several differences between callable and runnable:
①callable The method specified is call (), and runnable the method is run ().
A ②callable task can return a value after execution, whereas a runnable task cannot return a value
The ③call () method throws an exception, and the run () method cannot throw an exception.
④ run callable task to get a future object, future represents the result of an asynchronous computation. It provides a way to check whether the calculation is complete and so on
The completion of the calculation, and retrieve the results of the calculation. The future object allows you to understand the execution of the task, cancel the execution of the task, and get the results of the task execution
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service