Three ways to create threads in Java callable,runnable,thread comparison and usage

Source: Internet
Author: User
Tags thread class

Transferred from: http://www.chinaitlab.com/Java/line/942440.html

multithreaded programs are written in order to achieve multitasking concurrent execution, which enables better interaction with the user. There are generally three ways to thread,runnable,callable.

The difference between runnable and callable is that

(1) The method prescribed by callable is call (), and the method prescribed by Runnable is run ().

(2) Callable's task can return a value after execution, while Runnable's task is not to return a worthwhile

(3) Call method can throw an exception, the Run method can not

(4) Run the callable task to get a future object that represents the result of an asynchronous calculation. It provides a way to check whether the calculation is complete, to wait for the completion of the calculation, and to retrieve the results of the calculation. The future object enables you to understand task execution, cancel the execution of a task, and get execution results.

1. Create thread threads 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 from runnable:

Thread onethread = new Thread (onerunnable);

Step 4: Start the thread:

Onethread.start ();

At this point, a thread is created.

Note: The execution process of a thread is simple, when executing code onethread.start (), executes the Void run () in the Onerunnable object;

When the method executes, the thread dies.

2, similar to Method 1, by implementing the callable interface to create thread threads: where the Callable interface (and only one method) is defined as follows:

public interface callable<v>

{

V call () throws Exception;

}

Step 1: Create a class that implements the callable interface somecallable<integer> (slightly);

Step 2: Create a Class object:

callable<integer> onecallable = new somecallable<integer> ();

Step 3: Create a Futuretask<integer> object from callable<integer>:

futuretask<integer> OneTask = new futuretask<integer> (onecallable);

Note:futuretask<integer> is a wrapper that is created by accepting Callable<integer>, which implements both the future and the Runnable interface.

Step 4: Create a Thread object from futuretask<integer>:

Thread onethread = new Thread (onetask);

Step 5: Start the thread:

Onethread.start ();

At this point, a thread is 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 for the subclass:

Somethread onethread = new Somethread ();

Step 3: Start the thread:

Onethread.start ();

At this point, a thread is created to complete the

Three ways to create threads in Java callable,runnable,thread comparison and usage

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.