Simple summary of JAVA threads and thread pools

Source: Internet
Author: User

How Java threads are created:

1. inherit the thread class to create a threading class

Inherit the thread class and override the Run method of the class, which represents the task that the thread will complete.

2. Creating thread classes through the Runnable interface

Implement the Runnable interface, overriding the run () method of the interface, and the method body of the run () method is also the thread execution body of the thread. Create a thread object with the Runnable implementation class instance as the target of the thread, which is the true thread object.

3. Create threads through callable and future

(1) Implement the callable interface, overriding the call () method, which will act as the thread execution body and have a return value.

(2) Use the Futuretask class to wrap the callable object.

(3) Use the Futuretask object as the target of the thread object to create and start a new thread.

(4) Call the Get () method of the Futuretask object to get the return value after the child thread execution ends.

Java thread pool:

Newsinglethreadexecutor

Newfixedthreadpool

Newcachedthreadpool

Newscheduledthreadpool
The Java thread pool is actually a producer and consumer model

Simple summary of JAVA threads and thread pools

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.