"Good programmer training Camp"-java multi-Threading with concurrent (ii) thread synchronization

Source: Internet
Author: User

Android Training--My Java notes, looking forward to communicating with you!

Thread synchronization

1. Multi-threaded shared data
In multithreaded operations, it is possible for multiple threads to work on the same resource at the same time, which is shared data in multiple threads. To give a less appropriate example of a simple understanding, the picture is a small new home toilet manger, but there is only one home that is a "resource", then everyone in the family share this same "resource", that is, the so-called multi-threaded shared data

Can obviously see the problem of multi-threaded shared data, that will cause the uncertainty of the data! Just like small new is on the toilet, when the small new father came, at this time equivalent to a resource of two people in the Rob, then this will be a problem, this is what we usually say thread is not safe, we have to solve this thread unsafe problem!

2. Thread synchronization
To resolve data sharing problems, you must use synchronization, which means that multiple threads can only have one thread executing the specified code within the same time period, and other threads will wait until the thread finishes before they can continue execution

Threads to synchronize, there are two ways to do this:

(1) Synchronizing code blocks
This method wraps the action that needs to be synchronized, in which the synchronized object can be any object, in general, the current object, which can be understood as long as the object in the incoming synchronization code block is the same, all other methods cannot access the data in the Synchronized Code block (Resource)

synchronized(要同步的对象){        //要同步的操作}

(2) Synchronization method
Synchronization method oriented synchronization object is itself, that is, if there are two synchronous methods in the same class, when executing one of the synchronization methods, not only the resources in the synchronization method can not be used by external methods and the other (there may be more than one) can not be accessed, please understand this paragraph carefully!

publicmethod(){        //要同步的操作}

Synchronization criteria

There are several simple guidelines to follow when writing synchronized blocks, which can be helpful in avoiding the risk of deadlocks and performance hazards

(1) Keep the code block concise. Move out of the synchronized block with preprocessing and post-processing that do not change with the thread.
(2) do not block. such as Inputstream.read ().
(3) In the case of holding the lock, do not invoke the synchronization method of the other object.

Lock locks

JDK1.5 later appeared a more convenient way to achieve synchronization-and the package (lock), it's higher performance, so-called performance is more flexible, the need to lock the place to lock, in need to remove the place can be removed, compare the two methods above, You must complete the corresponding code block or the appropriate method to remove the lock, which will inevitably lose some performance

class X {   private final ReentrantLock lock = new ReentrantLock();   ...   public void m() {      lock.lock();  // block until condition holds     try {       ... method body     } finally {       lock.unlock()     }   } }

Not to be continued ....

The above is purely personal opinion, if there are deficiencies in the hope that there is an expert point, will be grateful, if you like to Exchange learning experience please give me a message thank you.

Original articles, reproduced please indicate the source

"Good programmer training Camp"-java multi-Threading with concurrent (ii) thread synchronization

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.