Detailed usage of linkedblockingqueue and concurrent1_queue

Source: Internet
Author: User

1. Define blockingqueue <E>: Java. util. Concurrent

API description:

public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable

A node with any range based on connected nodes
Blocking queue. This queue sorts elements by FIFO (first-in-first-out. QueueHeaderIs the longest element in the queue. QueueTailIs the minimum time element in the queue. Insert the new element to the end of the queue, and retrieve the element in the queue header. The throughput of the link queue is usually higher than that of the array-Based Queue, but its predictable performance is lower in most concurrent applications. Optional capacity range constructor parameters can be used to prevent queue overuse. If no capacity is specified, it is equal
Integer.MAX_VALUE. Unless a node is inserted, the queue exceeds the capacity. Otherwise, a link node is dynamically created after each insertion.

Benefits of blocking Queues: no extra synchronization is required for multi-thread operations on a common queue. In addition, the queue automatically balances the load, that is, there (both production and consumption) when processing is fast, it will be blocked, thus reducing the processing speed gap between the two sides.

 

2. concurrent1_queue <E>: Java. util. Concurrent
API description:

public class ConcurrentLinkedQueue<E>extends AbstractQueue<E>implements Queue<E>, Serializable

A connection node-based, unbounded, thread-safe queue. This queue sorts the elements according to the FIFO principle. QueueHeaderThe longest time element in the queue. QueueTailIs the minimum time element in the queue. Insert new elements to the end of the queue, and retrieve the elements from the queue header. When multiple threads share access to a public collection,Concurrent1_queue
Is an appropriate choice. This queue is not allowedNullElement.

 

Extension:

Execute cute (runnable). executor is applicable to the internal thread pool during execution to complete the operation. Executor uses an internal thread pool to complete operations during execution.

 

Create thread pool:

Executor executor = executors. newfixedthreadpool (10); // create a fixed number of thread pools

Executor executor = executors. newcachedthreadpool (); // create a cache-able thread pool. Calling execute will reuse previously constructed threads (such as available threads ). If the existing thread is not available. Create a new thread and add it to the pool.

 

Executorservice and lifecycle

Executorservice extends executor and adds some lifecycle management methods. The life cycle of an executor is in three states: running, closing, and terminating. Executor is created in running state. When executorservice. Shutdown () is called, The isshutdown method returns true out of the closed state. At this time, you should not want to add a task to the executor. After all the added tasks are executed, the executor returns true out of the termination state. if the executor is disabled, unchecked will be thrown when a task is submitted to the executor.
Exception rejectedexecutionexception.

API description:

public interface ExecutorService extends Executor

ExecutorProvides a method for managing termination and can be generated to track the execution status of one or more asynchronous tasks

Future. Can be disabledExecutorservice, Which causes it to stop accepting new tasks. After the program is closed, it is terminated. At this time, no task is being executed, No task is waiting for execution, and a new task cannot be submitted. After creation, return a message that can be used to cancel execution and/or wait for completion.

Future, MethodSubmitExtended Basic Methods
Executor.execute(java.lang.Runnable). MethodInvokeanyAnd
InvokeallIs the most common form of batch execution. They execute a set of tasks, and then wait for at least one or all tasks to complete (available
ExecutorCompletionServiceClass ).ExecutorsClass provides a factory method for the execution program service provided in this package.

 

VoidShutdown(): Start a closed sequence to execute previously submitted tasks, but do not accept new tasks. If it is disabled, the call has no other effect.

submit(Runnable task)Submit a runnable task for execution and return a future indicating the task.

 

 

 

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.