0

I am aware of the procedure to use the lambda expression to call the single arg constructor . But not sure how to invoke a constructor with additional arguments (not single arg). This can be achieved by using anonymous inner class as denoted in "How to call a specific parent constructor from anonymous inner class?" . But I am more interested in using the lambda expression.

For example: For calling the single arg constructor in Thread class, i can do this

Thread t1 = new Thread (() -> {
    //do sometask

});

But I could not find a similar way to call the Thread(String name) constructor.

basically I want to do something like below using lambda

Thread t2 = new Thread("Thread2") {
        @Override
        public void run() {                
        }

    };

Any help here is appreciated.Thanks.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
user2813978
  • 131
  • 3
  • 11
  • Your example is not clear. It is not calling a zero-args constructor of Thread - it calls a constructor with a single Runnable argument. And why would you need a lambda expression to call the Thread(String name) constructor? `String` is not a functional interface. – Eran Nov 21 '17 at 11:30
  • Your example is calling the public Thread(Runnable target) where runnable is the functional interface and because of that, you are able to use lambda in constructor. – Pulkownik Nov 21 '17 at 11:36
  • @Eran - Sorry for the confusion, edited the question now – user2813978 Nov 21 '17 at 11:39
  • 3
    It seems as if what you actually want is the two-argument constructor-- `Thread t1 = new Thread(() -> { doSomeTask(); }, "Thread1")` – slim Nov 21 '17 at 11:41

3 Answers3

2

I am not sure if I understand the question but if you want to use lambda and string constructor something like this should work:

Thread t2 = new Thread(() -> System.out.println("in "), "Thread2")

Because the constructor in Thread class looks like:

 public Thread(Runnable target, String name)

So as the first argument you can use lambda expression (functional interface) and the second argument is the name of the thread.

Pulkownik
  • 530
  • 1
  • 6
  • 21
1
Thread t1 = new Thread (() -> {
    //do sometask

}, "ThreadName");
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • This answers the question in the body, but not "How to call a specific parent constructor using Lambda?" – slim Nov 21 '17 at 11:51
  • @slim I think the title the OP meant to use was "How to call a specific parent constructor with Lambda as parameter?" which is closer to the question that was actually asked. – Leo Aso Nov 21 '17 at 12:02
  • @tobias_k I felt the code was fairly self-explanatory, and apparently the OP agrees. – Leo Aso Nov 21 '17 at 12:02
1

Your second example is not simply calling a constructor. It is defining an anonymous subclass of Thread, and calling its constructor.

Thread t2 = new Thread("Thread2") {
    @Override
    public void run() {
         // do something                
    }

};

Equivalent to:

class MyThread extends Thread {
    @Override
    public void run() {
         // do something                
    }
}

Thread t2 = new MyThread("Thread2");

So it is defining "stuff" about the new object in two ways:

  1. By passing the "Thread2" to the constructor
  2. By defining a subclass with an overridden method

A lambda can be used as an anonymous subclass (of a class) or implementation (of an interface) if the type is a functional interface, for example Callable:

Callable<Foo> r = () -> doSomething();

But Thread is not a functional interface -- it does not have exactly one abstract method -- so you can't do this for Thread.

For the specific case you have, Thread has a two argument constructor, so you don't need to create a subclass.

Thread t1 = new Thread( () -> someMethod(), "Thread1");
slim
  • 40,215
  • 13
  • 94
  • 127