Java multithreaded lock

Source: Internet
Author: User

JDK5 later provides a more flexible lock+condition mode for code synchronization, and a lock can bind multiple condition objects

1. Use the original synchronized modified or encapsulated code block with Lock.lock () and Lock.unlock () for manual lock acquisition and release

The original synchronization mode synchronized (obj) {...} JDK5.0 new Synchronization mode//lock.unlock (); It is advisable to put the finally execution try {lock.lock (); ...} finally {Lock.unlock ();}

2. Change the mode of communication between the original threads from the lock pair line obj.wait () and obj.notify () to the con.await () and Con.signal () methods of the Condition object

When overriding multi-producer multi-consumer mode with lock, the thread can specify to wake the producer or consumer, thus having greater efficiency and security

Package Jdk5lockdome;import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.lock;import java.util.concurrent.locks.reentrantlock;/** * @author wpy * */public class Producerconsumer {public static void main (STR Ing[] args) {Resource Resource = new Resource (); Producer Producer0 = new Producer (Resource); Producer producer1 = new Producer (Resource); Consumer consumer2 = new Consumer (Resource); Consumer Consumer3 = new Consumer (Resource); Thread t0 = new Thread (PRODUCER0); thread T1 = new Thread (producer1); Thread t2 = new Thread (CONSUMER2); thread t3 = new Thread (CONSUMER3); T0.start (); T1.start (); T2.start (); T3.start ();}} Class Producer implements Runnable {private Resource resource;public Producer (Resource Resource) {This.resource = Resourc e;} @Overridepublic void Run () {while (true) {Resource.set ("resource");}}} Class Consumer implements Runnable {private Resource resource;public Consumer (Resource Resource) {This.resource = Resourc e;} @Overridepublic void Run () {while (true) {RESOURCE.out ();}}} Class Resource {private String name;private int count = 1;//generated private Boolean flag = false;/** * Mutex (at the same time only one thread owns the lock) */p  Rivate Lock lock = new Reentrantlock ();p rivate Condition Producercon = lock.newcondition ();p rivate Condition Consumercon = Lock.newcondition ();/** * Production method * * @param name */public void set (String name) {try {lock.lock (); String threadname = Thread.CurrentThread (). GetName (); while (flag) {System.out.println (threadname + "Enter wait state"); Producercon.await ();} System.out.println (ThreadName + "Get Executive Right") THIS.name = name + count;count++; System.out.println ("Producer:" + this.name); flag = True;consumercon.signal ();} catch (Interruptedexception e) {e.printstacktrace ();} finally {Lock.unlock ();}} /** * Consumption method */public void out () {try {lock.lock (); String threadname = Thread.CurrentThread (). GetName (); while (!flag) {System.out.println (threadname + "Enter wait state"); Consumercon.await ();} System.out.println (ThreadName + "access to Executive power"); System.out.println ("============ Consumer:" + name); flag = False;producercoN.signal ();} catch (Interruptedexception e) {e.printstacktrace ();} finally {Lock.unlock ();}}}

  

  

Java multithreaded lock

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.