1

Take this simple piece of code

new Thread(() -> {

}).start();

I know it works, but how do I write my own class to allow someone to use it in the same manner?

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
John E.
  • 418
  • 2
  • 10

2 Answers2

3

Lambda works on Functional Interfaces, so if your class or method have ability to take such interface as parameter then you can use your class/method that manner.

new Thread(() -> {

}).start();

This code works as Thread has a overloaded constructor which take functional interface Runnable as arguments.

Example: How to write own class

Here DoWork is our functional Interface which has only abstract method doit

public interface DoWork {
    public  void doit(String str);
}

Let we have a class named MyClass whose constructor take DoWork as arguments and have a method startWork to start the work(Ordinary Method).

public class MyClass {
    DoWork dw;
    public MyClass(DoWork dw) {
        this.dw = dw;
    }
    public void startWork(String s){
        dw.doit(s);
    }

}

That's all, we can test it in Main

class Main {

    public static void main(String[] args) {

        new MyClass(str -> System.out.println(str)).startWork("Hello print it!!!");
    }

}

We can also use lambda with method parameter which take functional interface.

class Main {

    public static void main(String[] args) {

        test(str ->System.out.println(str), "Hello world!!!");

    }

    public static void test(DoWork d, String str) {
        d.doit(str);
    }

}

For more about Lambda you can see http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
1

This is the full code:

new Thread(new Runnable() {
    @Override
    public void run() {
         //do things
    }
}).start();

Lambda expressions let you express instances of single-method classes more compactly.

https://stackoverflow.com/users/5221149/andreas noticed that deep down there is a difference:

The anonymous class declaration still creates a separate anonymous class, while the lambda adds a hidden method and uses invokedynamic, basically using method reference, so there is a difference behind the scene. Functionally they are equivalent, though.

Community
  • 1
  • 1
nbokmans
  • 5,492
  • 4
  • 35
  • 59
  • sorry I mean in java 8 not older versions – John E. Oct 08 '15 at 21:49
  • Lambda expressions let you express instances of single-method classes more compactly. There is nothing else going on. – nbokmans Oct 08 '15 at 21:51
  • 2
    What this means is that, if you want to be able to use a lambda in your own functions, they just need to have a parameter whose type is a class or interface with exactly one abstract method, known as a [Functional Interface](https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html) – gla3dr Oct 08 '15 at 22:08
  • 1
    1) You have a close-parenthesis too many on `new Runnable()`. 2) The anonymous class declaration still creates a separate anonymous class, while the lambda adds a hidden method and uses `invokedynamic`, basically using method reference, so there *is* a difference behind the scene. Functionally they are equivalent, though. – Andreas Oct 08 '15 at 22:09
  • Thanks for noticing the parenthesis. I didn't know about the `invokedynamic`. – nbokmans Oct 08 '15 at 22:11
  • 2
    @nbokmans Yeah, I thought the same as you, until I noticed that lambdas didn't create a `$1` class file. Disassembler told the rest of the story. – Andreas Oct 08 '15 at 22:14
  • with your guys help I figured how to do it but it only seems to work with an interface not a class. Is there a function pointer in Java? – John E. Oct 08 '15 at 22:22
  • @John E.: maybe you should check [the tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html) first. – Holger Oct 09 '15 at 08:26
  • @Andreas: not only the compiled code differs, there are [even more differences at runtime](http://stackoverflow.com/q/27524445/2711488)… – Holger Oct 09 '15 at 08:26