Synchronized and Reentrantlock synchronization code block simple implementation

Source: Internet
Author: User

Synchronized synchronizing code blocks:

Package Net.flyingfat.lock;public class SyncLock {public static void main (String args[]) {final SyncLock lock=new SyncLock (); Thread.CurrentThread (). SetName ("main"); new Thread (New Runnable () {public void run () {Thread.CurrentThread (). SetName ( "Sub"); try {thread.sleep (+); lock.run2 ();} catch (Exception e) {e.printstacktrace ();}}). Start (); Lock.run2 ();} Public synchronized void Run2 () {try{system.out.println ("thread" +thread.currentthread (). GetName () + ": Get Object Lock" if (Thread.CurrentThread (). GetName (). Equals ("main")) {System.out.println ("main Thread block, Release object lock"); This.wait (); The main thread executes until this is blocked, and the object lock is freed, allowing the sub thread to enter the method System.out.println ("Main thread is Notifyed,get object lock again");}  else if (Thread.CurrentThread (). GetName () Equals ("sub")) {this.notify ();//sub thread gets the object lock released by the main thread, Wakes up the blocked main thread so that it executes System.out.println ("Sub thread Notify");} System.out.println (Thread.CurrentThread (). GetName () + ": Execute Over,release Object lock"); catch (Exception e) {e.printstacktrace ();}}}

Reentrantlock Synchronous Code implementation:

Package Net.flyingfat.lock;import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.reentrantlock;public class Reenlock {reentrantlock lock=new ReentrantLock (); Condition condition=lock.newcondition ();p ublic static void Main (string[] args) {thread.currentthread (). SetName ("Main "); final Reenlock test4=new Reenlock (); New Thread (New Runnable () {public void run () {try {thread.sleep (); Test4.run () ;} catch (Interruptedexception e) {e.printstacktrace ();}}}, "Sub"). Start (); Test4.run ();} public void Run () {try{lock.lock (); System.out.println ("Thread:" +thread.currentthread (). GetName () + "get Lock"), if (Thread.CurrentThread (). GetName (). Equals ("main")) {System.out.println ("Main thread block, release object lock"); condition.await ();//main thread executes until it blocks, And frees the object lock, allowing the sub thread to enter the method System.out.println ("Main thread is Signaled,get object lock again");}  if (Thread.CurrentThread (). GetName () Equals ("sub")) {condition.signal ();//sub thread gets the object lock released by the main thread, Wakes up the blocked main thread and makes it perform System.out.println ("Sub thread sigNal ");}} catch (Exception e) {e.printstacktrace ();} Finally{lock.unlock (); System.out.println ("Thread:" +thread.currentthread (). GetName () + "release lock");}} }


Synchronized and Reentrantlock synchronization code block simple implementation

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.