Java REVIEW-BASIC3

Source: Internet
Author: User

mutexes, Readwritelock, Arrayblockingqueue, Thread pools, LinkedList vs ArrayList, Object Pooling, Read-modify-write, JAV A.util.concurrent, Java.util.concurrent.atomic, CAS, collections, ADT, Java 5 generics

1. mutexes and Semaphore:

A Mutex provides mutual exclusion, either producer or consumer can has the key (mutex) and proceed with their work. As long as the buffer is filled by producer, the consumer needs to wait, and vice versa. At any point of time, only one thread can work with the entire buffer. The concept can be generalized using semaphore.

Using Semaphore (spilt resource as small part): A Semaphore is a generalized mutex. In lieu of single buffer, we can split the 4 KB to four 1 KB buffers (identical resources). A semaphore can associated with these four buffers. The consumer and producer can work in different buffers at the same time.

Personal understanding: These two words must be read, heard must know

Mutex: It can be understood as a key that the thread can access the same resource only if it has the key. And in this process, only one thread can access the current resource.

Semaphore: He is a broader mutex that divides resources into smaller parts, and then different lines rountines accesses access to their separate parts.

2.ReadWriteLock:

A Java.util.concurrent.locks.ReadWriteLock is an advanced thread lock mechanism. It allows multiple threads to read a certain resource, and only one-to-write it, at a time. Readwritelock Locking rules The rules by which a thread was allowed to lock the readwritelock either for reading or writing The guarded resource, is as follows:

Read Lock

If no threads has locked the readwritelock for writing, and no thread has requested a write lock (but not yet obtained I T). Thus, multiple threads can lock the lock for reading.

Write Lock

If No threads is reading or writing. Thus, only one thread at a time can lock the lock for writing.

Personal Understanding:

Readwritelock is a thread lock mechanism that allows a resource to be read by multiple resources at the same time, and a resource writes:

1). When no resource is written and no write lock is requested, it can be read by multiple resources

2). When there is no writing or reading, the thread can request a write lock.

3. Arrayblockingqueue

Arrayblockingqueue is a bounded, blocking queue this stores the elements internally in an array. That it's bounded means that it cannot store unlimited amounts of elements. There is a upper bound on the number of elements it can store at the same time. You set the upper bound at instantiation time, and then it cannot be changed. The Arrayblockingqueue stores the elements internally in FIFO (first-in, first-out) order. The head of the queue is the element which have been in queue the longest time, and the tail of the "the" is the element wh Ich have been in the queue the shortest time.

Personal Understanding:

Arrayblockingqueue:

He is a queue of fixed size (fixed sizes) that takes FIFO first, the content at the top is the oldest, the end of the content is the latest

4. Thread Pool:

Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. This kind of thread exists separately from the Runnable and callable tasks it executes and are often used to execute Multip Le tasks.

Using worker threads minimizes the overhead due to thread creation. Thread objects use a significant amount of memory, and in a large-scale application, allocating and de-allocating many THR EAD objects creates a significant memory management overhead.

One common type of thread pool is the fixed thread pool. This type of pool always has a specified number of threads running; If a thread is an somehow terminated while it's still in use, it's automatically replaced with a new thread. Tasks is submitted to the pool via an internal queue, which holds extra tasks whenever there is more active tasks than t Hreads.

An important advantage of the fixed thread pool was that applications using it degrade gracefully. To understand the consider a Web server application where each HTTP request is handled by a separate thread. If the application simply creates a new thread for every new HTTP request, and the system receives more requests than it C An handle immediately, the application would suddenly stop responding to all requests when the overhead of all those thread s exceed the capacity of the system. With a limit in the number of the threads that can be created, the application would not be servicing HTTP requests as Quic Kly as they come in, but it'll be servicing them as quickly as the system can sustain.

Personal Understanding:

The thread pool, which is composed of worker tread, typically uses the Fixed-size pool in order to reduce the overhead of creating and destroying threads, and the task is submitted to the pool's internal sequence.

For HTTP requests, there is a queue of requests to put them in the pool of threads, ensuring that a fixed thread is running on the system so that it does not crash.

6. LinkedList vs ArrayList:

1) search:arraylist search operation is pretty fast compared to the LinkedList search operation. Get (int index) in ArrayList gives the performance of O (1) While LinkedList performance is O (n).

2) deletion:linkedlist remove operation gives O (1) performance while ArrayList gives variable performance:o (n) in worst C An ASE (while removing first element) and an O (1) in the best case (while removing the last element).

3) Inserts Performance:linkedlist Add method gives O (1) performance while ArrayList gives O (n) in worst case. Reason is same as explained for remove.

4) Memory Overhead:arraylist maintains indexes and element data while LinkedList maintains element data and both pointers F or neighbor nodes hence the memory consumption is high in linkedlist comparatively.

Personal understanding: This question is about the difference between a linked list and an array:

1. The search efficiency array is higher because he can access it through index

2. Insert Delete Efficiency list higher because it does not affect other elements

3. Increase the efficiency list higher because there is no need to copy the original element to the new address

7. Object Pooling:

1) An object pool is a collection of a particular object, an application would create and keep on hand for those Situati The ONS where creating each instance is expensive. A good example would be a database connection or a worker thread. The pool checks instances in and out for users like books out of a library.

2) Usually object pooling is handled by a Java EE application server. If you are need to do it yourself, the best to use something like Apache's object pool. Don ' t write one yourself; Thread safety and other issues can make it complicated.

Personal Understanding:

An object pool is a collection of special objects that are typically expensive to create, similar to database connection pooling, and usually do not need to be created on their own because of thread-safety concerns.

8. Read-modify-write:

Read-modify-write is a class of atomic operations (such as Test-and-set, Fetch-and-add, and Compare-and-swap) that both re Ad a memory location and write a new value to it simultaneously, either with a completely new value or some function of The previous value. These operations prevent race conditions in multi-threaded applications. Typically they is used to implement mutexes or semaphores. These atomic operations is also heavily used in non-blocking synchronization.

Personal Understanding:

Combine reading and writing into a atomic operation to prevent race condition

9.CAS

CAS is generally much faster than locking, but it does depend on the degree of contention. Because CAS may force a retry if the value changes between reading and comparing, a thread can theoretically get stuck in A busy-wait if the variable in question is being hits hard by many other threads (or if it's expensive to compute a new VA Lue from the old value (or both)). The main issue with CAS was that it was much more difficult to program with correctly than locking. Mind you, locking are, in turn, much harder to use correctly than message-passing or STM, so don ' t take this as a ringing E Ndorsement for the use of locks.

CAS is the foundation of Java.concurrent.

CAS has 3 operands, a memory value of V, an old expected value of a, and a new value to be modified B. If and only if the expected value A and the memory value of the V phase, the memory value of V is modified to B, otherwise do nothing.

Java REVIEW-BASIC3

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.