1

ThreadWorker does not execute when I use method reference for creating new object in the constructor of Thread or passing Lambda for creation of new Object.

But it works fine when I create ThreadWorker object separately and I pass that to Thread class.

public class RunnableImpl {
    public static void main(String[] args) throws InterruptedException {
        ThreadWorker th= new ThreadWorker();
        Thread t1 = new Thread(th);
        t1.start();
        t1.join();
        System.out.println("Main method terminated");
    }
}

class ThreadWorker implements Runnable {

    @Override
    public void run() {
        int[] arr = { 1, 4, 8, 9, 1, 0, 4, 5, 4 };
        System.out.println(Arrays.stream(arr).sum());
    }
}

If I use lambda for example:

Thread t1 = new Thread(ThreadWorker :: new);

or

Thread t1 = new Thread(() ->new ThreadWorker());

then there is no output but If I create ThreadWorker Object separately then the program is working fine.

Can someone please let me know, how it is possible?

1
  • 2
    Thread expects a runnable Object, not a lambda to create a runnable Object. Use new ThreadWorker() insted of () ->new ThreadWorker() or ThreadWorker :: new. Commented Jun 3, 2019 at 7:18

1 Answer 1

6

Both

new Thread(() -> new ThreadWorker());

and

new Thread(ThreadWorker::new);

create a Thread whose Runnable instance's run() method simply creates a ThreadWorker instance, and does nothing with it. The run() method of ThreadWorker is not executed.

They are equivalent to passing the following anonymous class instance:

Thread t = new Thread(new Runnable() {
                                         public void run() {
                                             new ThreadWorker ();
                                         }
                                     });

If you want to use a lambda expression, you need:

new Thread(() -> new ThreadWorker().run());

If you want a method reference, you need:

new Thread(new ThreadWorker()::run);

That said, the following is much simpler:

new Thread(new ThreadWorker());
Sign up to request clarification or add additional context in comments.

6 Comments

Didnt know about last variant thus +1 (which seams kind of obvious as you can reference instance methods no problem)
But in this case,ThreadWorker th= new ThreadWorker();Thread t1 = new Thread(th); we are doing the same thing, creating Object and then passing to Thread class.
@AtishayJain In the case of Thread t1 = new Thread(th);, you are passing an instance of ThreadWorker, which implements Runnable, so the thread will execute its run() method when started.
@AtishayJain No, () -> new ThreadWorker() is not the same as new ThreadWorker(). One is an instance of ThreadWorker, which implements Runnable. The other is a lambda expression implementing a functional interface whose method's body creates a ThreadWorker instance. That lambda expression happens to fit the Runnable interface, which allows you to pass it to the Thread constructor, but it behaves differently.
@Eran I got it now, So in short Lambda is a method body, like the action which can be performed and it will be performed by a functional interface method which is run() method in this case. Thanks a ton for your quick help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.