Java Multithreading Learning Summary

Source: Internet
Author: User

What is a thread: A program performs multiple tasks, and each task is called a thread. multithreaded programs are programs that can run more than one thread at a time .

Specific understanding: Running multiple threads at the same time, multiple threads processing the same data resource may result in distortion of data, for example: <java core Technology. Volume One >p640 page bank transfer money problem. Because of improper management of threads, The error that caused the data. So there's a series of tricks for managing threads.

one. Thread Synchronization

1. Lock Object

there are two mechanisms to prevent code from being disturbed by concurrent access, Java provides the Synchronized keyword, and java5.0

introduced Reentrantlock class (function: To construct a reentrant lock that can be used to protect a critical section).

the basic structure of the Reentrantlock Protection code block is as follows:

Private Lock Mylock = new Reentrantlock (); Mylock.lock (); try {}finally {mylock.unlock ();}

The unlock operation is placed in the finally sentence to prevent the code in the critical section from throwing an exception, the lock must be released, or the thread will always be blocked.

Perhaps, if you create multiple objects, multiple objects get different lock objects, and multiple threads do not block?

A lock is reentrant because the thread can repeatedly obtain a lock that has already been held. The lock keeps a hold count to track nested calls to the lock method. and the Lock () method's function is to acquire this lock, which is blocked if it is owned by another thread at the same time .

2. Condition object:

The thread enters the critical section, but finds that he can perform after a certain condition is met. To use a conditional object to manage threads that have acquired a lock but are not able to do useful work .

Private Lock Mylock = new Reentrantlock ();p rivate Condition Condition = Mylock.newcondition (); Get Conditional Object

It's plain that reentrantlock can only lock a thread, and the conditional object can perform some action on the thread, such as await () put the process into the conditional wait set, Signallall () release the blocking state of all the threads in the conditional wait set, signal () Randomly selects a thread from the conditional wait set and unlocks the blocking state.

3.synchronized Keywords

Synchronous Blocking:

Private Object lock = new Object ();  An additional atomic operation is achieved through an object lock synchronized (lock) {     //multiple threads cannot collectively access the synchronized block content, only one thread can access the    Lock.notifyall () at the same time;   Wake up other threads to continue competing for synchronized block content}

4.volatile

volatile is a type modifier . It is designed to modify variables that are accessed and modified by different threads . volatile ensures that threads can read the values written by other threads correctly . The ref JMM Happens-before principle is visible, and multiple threads can collectively access the contents of this type variable.

5. Read/write lock

Java.util.concurrent.Locks defines two lock classes: Reentrantreadwritelock and reentrantlock.

Constructs a Reentrantreadwritelock object private reentrantreadwritelock rwl = new Reentrantreadwritelock ();//extract read lock and write lock private Lock Readlock = Rwl.readlock ();p rivate lock writelock = Rwl.writelock ();//read lock for all fetch methods: public double GetXXX () {    Readlo Ck.lock ();    try {  }    finally {    readlock.unlock ();}} Write lock on all modified methods: public double setxxx () {    writelock.lock ();    try {  }    finally {    writelock.unlock ();}}

lock Readlock ()//Gets a read lock that can be shared by multiple read operations, rejecting all write operations.

lock Writelock ()//Get a write lock, repel all other read operations and write operations

two. Blocking queues:

The difference between a blocking queue and a normal queue is that when the queue is empty, the operation to get elements from the queue is blocked, or when the queue is full, the operation to add elements to the queue is blocked. Threads that attempt to fetch elements from an empty blocking queue are blocked until other threads insert new elements into the empty queue. Similarly, threads that attempt to add new elements to the full blocking queue will also be blocked until other threads make the queue idle again .

three. Secure collection of Threads

four. Actuator:

thread Pool

five. Synchronizer:


Java Multithreading Learning Summary

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.