High concurrency programming for Java

Source: Internet
Author: User

First, the basic knowledge of multithreading

1.1 Introduction to processes and threads

How the program runs on the interface

1.4 java Sync synchronized keywords

Plus synchronous Format:

Synchronized ( requires an arbitrary object (lock) ) {

code block to put operations on shared data}

Synchronized is a keyword in Java, which is a feature built into the Java language. if a block of code is synchronized decorated, when a thread acquires the corresponding lock and executes the block, the other thread waits until the thread that acquires the lock releases the lock, and there are only two cases where the thread that gets the lock is released:

1. The thread that gets the lock executes the code block, and then the thread releases the possession of the lock

2. the thread execution exception occurs when the JVM causes the thread to automatically release the lock.

So synchronized waits for the thread to wait indefinitely: this is a fatal disadvantage

So the lock was introduced to solve the problem.

1.5 Lock

1.5. 1Lock in the selection of Lock and synchronized

1)Lock is an interface, and synchronized is the key word in Java,synchronized is a built-in language implementation;

2) lockunlock () to release the lock, it is likely to cause a deadlock phenomenon, so use lockfinally release lock in block;

3)lock allows the thread waiting for the lock to respond to interrupts, while synchronized does not , while using synchronized , the waiting thread waits and fails to respond to interrupts;

4) lock can be used to know if there is a successful acquisition of locks, and synchronized is unable to do .

5)Lock can improve the efficiency of multiple threads for read operations.

In terms of performance, if competitive resources are not intense, the performance of both is similar, and when competitive resources are very intense (that is, there are a lot of threads competing at the same time), then lock performance is far better than synchronized. Therefore, in the specific use, according to the appropriate circumstances to choose.

use of each method in the 1.5.2 Lock interface:

Lock (),trylock (),Trylock (long time, timeunit unit),lockinterruptibly () are used to acquire locks.

The UnLock () method is used to release the lock.

1.5. The difference between the 34 methods of acquiring a lock:

The lock () method is usually done using the try{}catch{} block, and the action of releasing the lock is placed in the finally block to ensure that the lock must be released to prevent the deadlock from occurring.

The Trylock () method is a return value that represents the attempt to acquire a lock, returns true if the fetch succeeds, or False if the fetch failed (that is, the lock was fetched by another thread) . It is said that this method will return at once in any case. You won't be waiting there until you get the lock.

Trylock (long time, timeunit unit) method and trylock () falsetrue

The lockinterruptibly () method is special, and when the lock is acquired by this method, if the thread is waiting to acquire the lock, the thread can respond to the interrupt, that is, the wait state of the break thread. It is also said that when two threads pass through lock.lockinterruptibly () to acquire a lock, if thread A acquires a lock at this point and thread b waits only, then thread b Call the threadb.interrupt () method to be able to wait in thread B .

Note that when a thread acquires a lock, it is not interrupted by the interrupt () method.

Therefore, when a lock is acquired through the lockinterruptibly () method, it can be interrupted if it cannot be obtained and only waits.

And with synchronized modification, when a thread waits for a lock, it cannot be interrupted, only waiting. Reentrantlock

Using the lock interface directly, we need to implement a lot of methods, inconvenient,Reentrantlock is the only class that implements the lock interface, and Reentrantlock Provides more methods,reentrantlock, meaning "can be re-entered lock".

High concurrency programming for Java

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.