Java multi-thread control

Source: Internet
Author: User

 

Multithreading is not familiar to many programmers.

In earlier Java versions, a task was started before Java 5.0 by calling the START () method of the thread class, the task is submitted and executed at the same time. If you want to schedule the task execution, or want to control the number of concurrent threads, you need to write additional code to complete it. Java 5.0 provides a new job execution architecture that allows us to easily schedule and control job execution. This architecture mainly consists of three interfaces and their corresponding classes. These three interfaces are:

Executor,

Exevutirservice

Scheduledexecutorservice

The following is a detailed description.

Executor interface: used to execute a runnable task. Its value defines a method:

Execute (runnable command): executes a ruannable task.

Executorservice interface: executorservice inherits the executor method and provides services for executing callable tasks and Middle-finger tasks. The methods defined mainly include:

Submit (task): Used to submit a callable or runnable task and return a future object representing the task.

Invokeall (collection of tasks): a collection of batch processing tasks and a collection of future Objects representing these tasks.

Shutdown (): closes the service after the submitted task is completed and no longer accepts the new task.

Shutdownnow (): stops all ongoing tasks and closes the service.

Isterminated (): test whether all tasks have been completed.

Isshutdown (): test whether the executorservice has been disabled

Scheduledexecutorservice interface:

Based on executorservice, scheduledexecutorservice provides the time-based task execution function. It provides the following methods:

Schedule (task,
Initdelay): schedules the submitted callable or runnable tasks to be executed after the time specified by initdelay.

Scheduleatfixedrate (): Schedule the submitted runnable tasks to be executed repeatedly at the specified interval.

Schedulewithfixeddelay (): Schedule the submitted runnable task to be executed again after each execution, waiting for the time specified by delay

_________________________________________________________________________________________________________________________________

Although the interface mentioned above has its implementation class, we recommend that you use the tool class of executors to obtain the specific objects of the executor interface for convenience. It should be noted that executors is a class, it is not the plural form of executor. executor provides some static methods:

Callable (runnable task): converts a runnable task to a callable task.

Newsinglethreadexecutor:
Generates an executorservice object. This object has only one thread to execute the task. If there are more than one task, the task will be executed in sequence.

Newcachedthreadpool ():
Generates an executorservice object with a thread pool. The size of the thread pool is adjusted as needed. After the thread executes the task, it returns the thread pool for the next task.

Newfixedthreadpool (int
Poolsize): generates an executorservice object with a poolsize thread pool. If the number of tasks is greater than poolsize, the task will be executed in a queue in sequence.

Newsinglethreadscheduledexecutor: generates a scheduledexecutorservice object. The thread pool size of this object is 1. If there are more than one task, the task will be executed in sequence.

Newscheduledthreadpool (int
Poolsize): generates a scheduledexecutorservice object. The thread pool size of this object is poolsize. If the number of tasks is greater than poolsize, the task will be waiting for execution in a queue.

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.