Atitit. java queue System and custom database-Based queue summary o7t and atitito7t

Source: Internet
Author: User

Atitit. java queue System and custom database-Based queue summary o7t and atitito7t

Atitit. java queue System and custom database-Based queue summary o7t

1. Blocking queue and non-blocking queue 1

2. java. util. Queue interface, 1

3. concurrent1_queue 2

4. BlockingQueue blocking queue 2

4.1. 1. ArrayBlockingQueue 3

4.2. Define blockingqueue 3

4.3. 3. DelayQueue 3

4.4. 4. PriorityBlockingQueue 3

4.5. SynchronousQueue 3

5. Blocking blockingdeque is blocking Double-end queue 3

6. Customize the Queue to implement the Queue interface 4

7. Refer to 4

 

1. Blocking queue and non-blocking queue

The data structure of most production and consumption models is queue. The thread-safe Queue provided by Java can be divided into blocking Queue and non-blocking Queue. A typical example of blocking Queue is BlockingQueue, and a typical example of non-blocking Queue is concurrent1_queue, in actual applications, You must select a blocking queue or a non-blocking Queue Based on your actual needs.

Synchronous mode: blocking mode; asynchronous mode: non-blocking mode

 

Author: old wow's paw Attilax iron, EMAIL: 1466519819@qq.com

Reprinted please indicate Source: http://blog.csdn.net/attilax

 

2. java. util. Queue interface,

 

Added the java. util. Queue interface in java 5,

 

It is used to support common queue operations. This interface extends the java. util. Collection interface.
When using Queue, try to avoid the add () and remove () methods of Collection. Instead, use offer () to add elements and poll () to obtain and remove elements. Their Advantages
The point is to determine whether the operation is successful or not through the return value. The add () and remove () Methods throw an exception when the operation fails. If you want to use the front-end without removing this element, use
Element () or peek () method.
It is worth noting that the Queue list class implements the Queue interface, so we can use the Queue list as a Queue.

3. concurrent1_queue4. BlockingQueue blocking queue

BlockingQueue not only implements the basic functions of a complete queue, but also automatically manages the automatic wait and wake-up functions between multiple threads in a multi-threaded environment, so that programmers can ignore these details, focus on more advanced features.

Blocking is usually implemented through locking...

Common BlockingQueue


After learning about the basic functions of BlockingQueue, let's take a look at which members of the BlockingQueue family are there?

 

First, let's take a look at the common methods provided by BlockingQueue:
 

An exception may be reported.

Returns a Boolean value.

Possible Blocking

Set the wait time

Join

Add (e)

Offer (e)

Put (e)

Offer (e, timeout, unit)

Team-out

Remove ()

Poll ()

Take ()

Poll (timeout, unit)

View

Element ()

Peek ()

None

None

·
From the table above, we can clearly see the role of each method. The add (e) remove () element () method does not block the thread. If the constraints are not met, an IllegalStateException is thrown. For example, when the queue is filled with elements and add (e) is called, an exception is thrown.

· The offer (e) poll () peek () method does not block the thread or throw an exception. For example, when the queue is filled with elements and offer (e) is called, no element is inserted. The function returns false.

· To implement the blocking function, call the put (e) take () method. When the constraints are not met, the thread will be blocked.



BlockingQueue

4.1. 1. ArrayBlockingQueue4.2. 2. Define blockingqueue

Linked list-based blocking queue

4.3. 3. DelayQueue

The DelayQueue element can be obtained from the queue only when the specified delay time is reached.

4.4. 4. PriorityBlockingQueue

Priority-based blocking Queue (Priority judgment is determined by the Compator object passed in by the constructor). However, PriorityBlockingQueue does not block data producers, the consumer will only block the data when there is no consumable data. Therefore, you must pay special attention to the fact that the producer's data production speed cannot be faster than the consumer's data consumption speed. Otherwise, after a long time, all available heap memory spaces will be exhausted. When PriorityBlockingQueue is implemented, the Internal Control thread synchronization lock adopts a fair lock.

4.5. SynchronousQueue5. blocked blockingdeque is blocking Double-end queues

ArrayDeque bidirectional queue
LinkedBlockingDeque blocks double-end queues
ArrayBlockingQueue bidirectional concurrency blocking queue
LinkedBlockingQueue FIFO queue
Concurrentincluqueue: connection node-based unbounded thread security queue
PriorityBlockingQueue unbounded blocking queue with priority
There are many more, you can look at the implementation classes of AbstractQueue and Deque.

 

6. Customize the Queue to implement the Queue Interface

Implement the add, remove (obj) method...

And batch operation method... addBatch, peekBatch,

7. Reference

Java thread team blockingqueueusage -shwenwen-itpubboke .htm

Synchronous queue SynchronousQueue implementation in Java concurrent packages _ Concurrent Programming Network-ifeve.com.htm

Java multi-thread Summary-thread security Queue-logs of firewood cotton-wangyi Boke .htm


Java programming: defines a generic Queue class, which performs inbound and outbound Queue operations on the String and Integer objects respectively.

Import java. util. ArrayList;
Import java. util. Collections list;
Import java. util. List;

Public class Queue <T> {
Private static listing list q;
Private T t;
Private T getT (T t ){
Return t;
}
Public void setT (T t ){
This. t = t;
}
Public Queue (T t ){
This. t = t;
}
Private void offer (){
If (q! = Null ){
Q. offer (t );
}
Else {
Q = new vertex list ();
Q. offer (t );
}
}
Public List poll (){
List list = null;
While (q! = Null & q. size ()> 0 ){
List = new ArrayList ();
List. add (q. poll ());
}
Return list;
} Public static void main (String [] args ){
Queue <Integer> q = new Queue <Integer> (1 );
Q. offer ();
List list = q. poll ();
System. out. println ("the output queue element is:" + list. get (0 ));
Queue <String> qq = new Queue <String> ("1 ");
Q. offer ();
List list2 = q. poll ();
System. out. println ("the output queue element is:" + list2.get (0 ));

}
// TODO Auto-generated method stub

}

The key to how java batch fetch data from the database into the queue and then process it one by one is the implementation of message queues.

Why message queue?
You only need the data structure of the queue.
You can use the built-in JDK to implement the Queue list.

Queue queue = new Queue list ();
Queue. add (Object) // add at the end

Queue. remove () // retrieve the header

You only need to add the objects retrieved from the database through JDBC to the queue in a loop, and then remove the objects in a loop.

Related Article

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.