Application Development, Product to Market
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use

Java 1.8 Lambda & Method Reference

2/25/2018

0 Comments

 
One way to create thread is to pass a object that implements Runnable Interface to a Thread constructor as mentioned in Java Multithreading Approaches (Java Concurrency - Part 1):
​public class MyRunnable implements Runnable{
  // this method is called when a runnable thread starts
  public void run(){
    printMessage();
  }
}

public class ThreadDemo{
  public static void main(String args[]){
    Thread t1 = new Thread(new MyRunnable); 
    Thread t2 = new Thread(new MyRunnable);
    Thread t3 = new Thread(new MyRunnable);
    t1.start();    
    t2.start();
    t3.start();

  }

  public static void printMessage(){
    System.out.println("Hello");
  }
}

Java 1.8 introduce Functional Interface. Any interface with a SAM (Single Abstract Method) is a functional interface, and its implementation may be written as a lambda expression. A Functional Interface can have many default methods or static methods declared, but can only have one single abstract method. 

You may use Lambda Expression in place of a Functional Interface. For example, Runnable interface is a Functional Interface. Let me use the Lambda Expression instead. In this approach, I no longer need to define MyRunnable.
public class ThreadDemo{
  public static void main(String args[]){
    Thread t1 = new Thread(() -> printMessage()); 
    Thread t2 = new Thread(() -> printMessage());
    Thread t3 = new Thread(() -> printMessage());
    t1.start();    
    t2.start();
    t3.start();

  }

  public static void printMessage(){
    System.out.println("Hello");
  }
}
A method reference is the shorthand/alternative syntax for a lambda expression that executes just ONE method. Replace,
() -> printMessage();
with,
ThreadDemo::printMessage
and get:
public class ThreadDemo{
  public static void main(String args[]){
    Thread t1 = new Thread(ThreadDemo::printMessage); 
    Thread t2 = new Thread(ThreadDemo::printMessage);
    Thread t3 = new Thread(ThreadDemo::printMessage);
    t1.start();    
    t2.start();
    t3.start();

  }

  public static void printMessage(){
    System.out.println("Hello");
  }
}
To take the Method Reference syntax one step further, as another example, you can replace Lambda Expression:
(p) -> System.out.println(p)
with,
System.out::println
as in:
public class MethodReferenceExample{
  public static void main(String[] args){
    List<Person> people = Arrays.asList(
      new Person("Charles", "Dickens", 60),
      new Person(Lewis", "Carroll", 42)
    );
    performConditionally(people, p -> true, System.out::println);
  }

  private static void performConditionally(List<Person> people, Predicate<Person> predicate, Consumer<Person> consumer)
    for (Person p: people){
      if (predicate.test(p)){
        consumer.accept(p);
      }
    }
}
0 Comments



Leave a Reply.

    Categories

    All
    Algorithm
    Concurrency
    CQ
    Data Structure
    Design Pattern
    Developer Tool
    Dynamic Programming
    Entrepreneur
    Functional Programming
    IDE
    Java
    JMX
    Marketing
    Marklogic
    Memory
    OSGI
    Performance
    Product
    Product Management
    Security
    Services
    Sling
    Social Media Programming
    Software Development

    Feed Widget

    Archives

    May 2020
    March 2020
    April 2018
    March 2018
    February 2018
    December 2017
    March 2017
    November 2016
    June 2016
    May 2016
    April 2016
    October 2015
    September 2015
    August 2015
    September 2014
    July 2014
    June 2014
    May 2014
    March 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013

    RSS Feed

in loving memory of my mother  and my 4th aunt
Photos used under Creative Commons from net_efekt, schani, visnup, Dan Zen, gadl, bobbigmac, Susana López-Urrutia, jwalsh, Philippe Put, michael pollak, oskay, Creative Tools, Violentz, Kyknoord, mobilyazilar