Lambda Threads in Android

More elegant multithreading.

You’ll need to install Retrolambda in Android Studio to use lambdas.

Example: without a lambda
new Thread(new Runnable() {
    public void run() {
        Log.d("THREAD", "Hello World\n");
    }
}).start();
Example: with a lambda
new Thread(() -> {
    Log.d("THREAD", "Hello World\n");
}).start();

I love creating and running threads “in-line” with just two lines of code.
It’s more readable than defining a Runnable in one place and inserting it into a Thread elsewhere.

For example,
public void sendHello() {

    doSomething();
    doMoreSomethings();

    new Thread(() -> {
        try {
            mOutputStream.write("Hello World!".getBytes());
            mOutputStream.flush();
        } catch (IOException e) {}
    }).start();

    doSomethingElse();
    doMoreSomethingElses();
}