Comparison and usage of Callable, Runnable, and Thread in java

Source: Internet
Author: User

Multi-threaded programs are written to achieve concurrent execution of multiple tasks, so as to better interact with users. Generally, there are three methods: Thread, Runnable, and Callable. the difference between Runnable and Callable is that (1) Callable specifies the call () method and Runnable specifies the run () method (). (2) Callable tasks can return values after execution, while Runnable tasks cannot return value (3) the call method can throw an exception and the run method cannot (4) run the Callable task to get a Future object, indicating the result of asynchronous calculation. It provides a method to check whether the computation is complete, waiting for the computation to complete and retrieving the computation results. You can use the Future object to understand the task execution status, cancel the task execution, and obtain the execution result. 1. Create a Thread by implementing the Runnable interface: Step 1: Create a class that implements the Runnable interface: class SomeRunnable implements Runnable {public void run () {// do something here} Step 2: Create a Class Object: Runnable oneRunnable = new SomeRunnable (); Step 3: Create a Thread object by Runnable: thread oneThread = new Thread (oneRunnable); Step 4: start Thread: oneThread. start (); so far, a thread has been created. Note: The thread execution process is very simple. When the code oneThread is executed. start ();, it will execute void run () in the oneRunnable object; method. After this method is completed, the thread will die. 2. Similar to method 1, The Callable interface is implemented to create a Thread. The Callable interface (or only one method) is defined as follows: public interface Callable <V> {V call () throws Exception;} Step 1: Create a class SomeCallable that implements the Callable interface <Integer> (omitted); Step 2: Create a class object: callable <Integer> oneCallable = new SomeCallable <Integer> (); Step 3: Create a FutureTask <Integer> object by Callable <Integer>: futureTask <Integer> oneTask = new FutureTask <Integer> (oneCallable); Note: FutureTask <Integer> is a package that accepts Callab Le <Integer>, which implements both the Future and Runnable interfaces. Step 4: Create a Thread object by FutureTask <Integer>: Thread oneThread = new Thread (oneTask); Step 5: start Thread: oneThread. start (); so far, a thread has been created. 3. Create a Thread by inheriting the Thread class: Step 1: Define a subclass that inherits the Thread class: class SomeThead extends Thraad {public void run () {// do something here} Step 2: Construct an object of the subclass: SomeThread oneThread = new SomeThread (); Step 3: start thread: oneThread. start (); so far, a thread has been created.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.