Java8 's lambdas expression simulates an AOP idea, encapsulating the lock function

Source: Internet
Author: User

Java8 's lambdas, in fact, will become a method in this class after compiling, lambdas expression is just a simple syntax. The lambdas expression is actually a function pointer in the simulated c,c++ language, both of which can pass functions as arguments to the method, but not in Java, because everything in Java is a class, and the method must be dependent on the class to survive.

This is unavoidable java8 the anonymous class that appeared before. However, Java8 simplifies the syntax, but there is a substantial difference from the naming class. We can use the AOP idea to encapsulate common logic, the non-business logic that the client does not have to consider, and those business logic that can be passed in through the lambdas expression.

Now take a look at the embodiment code of this AOP idea in Reentrantreadwritelock:


package com.doctor.java8;import java.util.arraydeque;import java.util.deque;import  java.util.list;import java.util.concurrent.locks.lock;import  java.util.concurrent.locks.reentrantreadwritelock;import java.util.function.supplier;import  java.util.stream.collectors;/** * simplifying-readwritelock-with-java-8-and-lambdas  This is also the use of AOP programming ideas  *  *  @see  http://www.javacodegeeks.com/2014/03/ simplifying-readwritelock-with-java-8-and-lambdas.html *  *  @author  doctor *  *  @time  2015 April 20   PM 2:48:19 */public class  Simplifyingreadwritelockwithjava8andlambdas {public static void main (String[] args)  {mybuffer mybuffer = new mybuffer (2); Mybuffer.put ("name"), Mybuffer.put ("name1"); Mybuffer.put ("name2"); System.out.println (Mybuffer.getrecent ()); System.out.println (Mybuffer.getdiscardedcount ());} Private static clAss mybuffer {private final int capacity;private final deque<string>  recent;private int discarded;private final functionalreadwritelock guard;public  mybuffer (final int capacity)  {this.capacity = capacity;recent = new  ArrayDeque<String> (this.capacity);d iscarded = 0;guard = new  Functionalreadwritelock ();} Public void put (String value)  {guard.write (()  -> {while  (Recent.size ()  >= capacity)  {recent.removefirst ();d iscarded++;} Recent.add (value);});} Public list<string> getrecent ()  {return guard.read (this::d efensivecopyofrecent);} Private list<string> defensivecopyofrecent ()  {return recent.stream (). Collect ( Collectors.tolist ());} Public int getdiscardedcount ()  {return guard.read (()  -> {return  discarded;});} PublIc int gettotal ()  {return guard.read (()  -> {return discarded +  recent.size ();});} Public void flush ()  {guard.write (This::unsafeflush);} Private void unsafeflush ()  {discarded += recent.size (); Recent.clear ();}} private static class functionalreadwritelock {private final lock readlock; Private final lock writelock;public functionalreadwritelock ()  {this (new  Reentrantreadwritelock ());} Public functionalreadwritelock (Final reentrantreadwritelock readwritelock)  {readLock  = readwritelock.readlock (); Writelock = readwritelock.writelock ();} Public void read (runnable runnable)  {readlock.lock (); Try {runnable.run ();}  finally {readlock.unlock ();}} Public <t> t read (Supplier<t> supplier)  {readlock.lock (); Try {return  supplier.get ();}  Finally {readlock.unlock ();}} Public void write (runnable runnable)  {writelock.lock (); Try {runnable.run ();}  finally {writelock.unlock ();}} Public <t> t write (Supplier<t> supplier)  {writelock.lock (); try { Return supplier.get ();}  finally {writelock.unlock ();}}}


Java8 's lambdas expression simulates an AOP idea, encapsulating the lock function

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.