5

In Java8, lambda expressions were introduced. This question is about when parallel lambdas are executed.

Before Java8, Callable-classes were one way to execute multiple threads at one time. Callables can be used with Executor-classes to be executed. Let's assume I am using a Fixed Thread Pool, using 3 as number of active processing tasks. And let's assume I have 8 tasks. Fixed Thread Pool would start first three tasks, and as one finishes, next task is started, until all 8 tasks are finished.

What would happen, if I implement my tasks as Java8-lambdas? Would all 8 be started at once? Or sequentially? Or in any clever way?

In special, are they running in the same thread as the caller (without using an Exeuctor)? By their nature, I guess lambdas could be executed in another thread easily.

7
  • 3
    Lambdas are just another way of writing the same thing. It is just some piece of code that can be run, exactly like a Runnable that you would pass. I does not affect the way they are executed. Maybe I just don't understand your question. Commented May 6, 2014 at 13:00
  • Callables are given to an executor (where I have control of the thread-count), while lambda expressions are executed as soon as they are called(?!). I am asking myself, how lambda-expressions behave referring to multithreading. Commented May 6, 2014 at 13:04
  • while lambda expressions are executed as soon as they are called -- and they are called at the same moment as any other Callable would (They are not executed as soon as they are created). Commented May 6, 2014 at 13:05
  • 1
    You are confusing Lambdas with 'something that add more juice to your cherry'. Well they dont. They are a compile time or code level improvements not byte code level ones. Your code with or without Lambdas compiles to the same byte code (almost) and functions int he same way. So dont worry about it if you know the basics. Commented May 6, 2014 at 13:07
  • Callables are executed as soon as they are called. Commented May 6, 2014 at 13:17

1 Answer 1

12
Runnable r = () -> System.out.println("hello");

is equivalent to

Runnable r = new Runnable() {
    @Override
    public void run() {
        System.out.println("hello")
    }
};

And it doesn't change anything to how a runnable is executed. If you submit your runnable to a thread pool, then the thread pool will execute it, whatever you used to create your runnable: a lambda, an anonymous class, or a top-level class. In the end, what you define is an instance of Runnable, and that's the only thing which matters.

Sign up to request clarification or add additional context in comments.

3 Comments

So a lambda is not executed in an own thread; or it is only, if I give it to an Executor? I had hoped to have multi-threading as a gift by using lambdas.
@MarcusSchultö You can think of lambdas as just syntax sugar, an alternative, to replace the code with a lot of overhead that we used to write, as JBNizet has shown here. There is no difference in the way they are used.
@MarcusSchultö what you get for free, regarding multi-threading, is parallel streams: hugeList.parallelStream().map(s -> s.toUpperCase()).collect(toList()) for example would make all the strings uppercase, using multiple threads. But this is not stricto sensu linked to lambdas. Lambdas are just a great new way to create instances of the functions, predicates, consumers, etc. used by the new Stream API.

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.