What does thread safety mean?
Thread safety is the same as the result of each run and a single-threaded run, and the values of the other variables are the same as expected.
Thread safety means that multithreaded access to the same code does not produce indeterminate results.
Parallelism and concurrency differences
1, parallel refers to the two at the same time to carry out a thing, such as running, two people are constantly running forward;
2, concurrency refers to the situation of limited resources, the two alternate use of resources, such as a section of the road (single-core CPU resources) at the same time only one person, a go after a period, to b,b used to continue to a, alternating use, the purpose is to improve efficiency.
Linkedblockingqueue is a thread-safe blocking queue, the implementation of first-in and out-of-class features, is the preferred producer consumers, can specify the capacity, or can not be specified, the default is not specified the maximum is Integer.max_value, The main use of the put and take method, put a method put an object to the end of the queue, when the queue is full, will block until a queue member is consumed, take the method from the head of an object, when the queue is empty, will block until a queue member is put in.
Common methods are as follows:
Add (AnObject):
Add AnObject to Blockingqueue, add success to return true, and throw an exception if the Blockingqueue space is full.
Offer (AnObject):
Indicates that if possible, AnObject is added to Blockingqueue, which returns true if Blockingqueue can be accommodated, otherwise false is returned.
Put (AnObject):
Add AnObject to Blockingqueue, and if Blockingqueue has no space, the thread calling this method is blocked until there is room in the blockingqueue to continue.
Poll (time):
Gets and removes the header of this queue, and if it cannot be removed immediately, it can wait for the time specified in the Times parameter and return NULL if it is not taken.
Take ():
Gets the first object in the Blockingqueue, if the blockingqueue is empty, blocks into the wait state until Blockingqueue has a new object to join.
Clear ():
Completely removes all elements from the queue.
The Remove () method deletes the elements of the team header directly
Peek () method takes the elements of the team header directly and does not delete
Blocking Queue--linkedblockingqueue