The four & thread lock lock application for concurrent library application

Source: Internet
Author: User

In the JAVA5 thread concurrency library, we provide the corresponding thread lock interface lock to help us synchronize processing. Lock is more object-oriented than the synchronized in the traditional threading model, and the lock itself is an object, and the code executed by two threads implements the synchronous mutex effect, using the same lock object. The lock is to be on the internal method of the resource class to be manipulated, not in the thread code.

Java.util.concurrent.locks is a useful interface that is commonly used in concurrent programming.

| The----LOCK Implementation provides a wider range of locking operations than can be obtained using the synchronized methods and statements

|----Readwritelock maintains a pair of related locks , one for read-only operations and another for write operations

|----Condition The object Monitor method (Wait,notify, and notifyall) into distinct objects to c5> Lock Implementation combined, providing multiple waits for each object

 This article is mainly about the contents of lock, so the main description for the lock:

  Interface:

public interface Lock

  All known implementation classes:

Reentrantlock

  Reentrantreadwritelock.readlock

Reentrantreadwritelock.writelock

With the increase of flexibility, it also brings more responsibility. The lock auto-release feature that occurs when you use the Synchronized method and the statement is lost without using block structure locks. In most cases, the following statements should be used:

    Lock lk = ...;    Lk.lock ();     Try {        //  access the resource protected by this lock     finally  {        Lk.unlock ();    }

When locking and unlocking occurs in different scopes, care must be taken to ensure that all code executed while holding the lock is protected with try-finally or try-catch to ensure that the lock is released when necessary.

The print string is printed using the lock mechanism, so that its internal threads are mutually exclusive and the source code is as follows:

1 ImportJava.util.concurrent.ExecutorService;2 Importjava.util.concurrent.Executors;3 ImportJava.util.concurrent.locks.Lock;4 ImportJava.util.concurrent.locks.ReentrantLock;5 6  Public classLocktest {7      Public Static voidMain (string[] args) {8         FinalBusiness Business =NewBusiness ();9Executorservice executor = Executors.newfixedthreadpool (3);Ten          for(inti = 0; I < 3; i++) { One Executor.execute ( A                     NewRunnable () { -                          Public voidrun () { - Business.service (); the                         } -                     } -  -             ); +         } - Executor.shutdown (); +     } A  at     Private Static classBusiness { -         Private intcount; -Lock lock =NewReentrantlock (); -  -          Public voidService () { - Lock.lock (); in             Try { -count++; to                 Try { +Thread.Sleep (1); -}Catch(interruptedexception e) { the e.printstacktrace (); *                 } $ System.out.println (count);Panax Notoginseng}Catch(RuntimeException e) { - e.printstacktrace (); the}finally { + Lock.unlock (); A             } the         } +     } -}

The results of the operation are as follows:

Tips: more commonly used and important locks are read-write locks, see my next blog for more details: application of the V & Readwritelock scenario for concurrent library applications

The four & thread lock lock application for concurrent library application

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.