Because the compiler warns me Anonymous new Runnable () can be replaced with lambda

4

Because this code ( Thread ) shows this message ( in the compiler ), and how do you not show it again?

Code:

public void onCreate(Bundle icicle) {
    ...
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mediaPlayer) {

            (new Thread(new Runnable() { //(Anonymous new Runnable() can be replaced with lambda)
                @Override
                public void run() {
                    while (!Thread.interrupted() && isActive)
                        try {
                            Thread.sleep(5000);
                            runOnUiThread(new Runnable() //(Anonymous new Runnable() can be replaced with lambda)// start actions in UI thread
                                {
                                    @Override
                                    public void run() {
                                        Log.v("INFOS: THREAD", "Valor atribuido"); // this action have to be in UI thread
                                    }
                                });
                        } catch (InterruptedException e) {
                            // ooops
                        }
                }
            })).start(); // the while thread will start in BG thread
        }
    });
}

Message in compiler:

  

Anonymous new Runnable () can be replaced with lambda less ... (Ctrl + F1)   This inspection reports all anonymous classes which can be replaced   with Lambda expressions Lambda syntax is not supported under Java 1.7   or earlier JVMs.

Reference in the SOen .

How can I write this code so that it is correct ? The thread executes a command every x seconds if isActive true .

Attention to the question tags.

    
asked by anonymous 03.07.2015 / 01:46

1 answer

3

This is because in Java 8 we have the new lambda expressions syntax that simplifies the need to (ab) use of anonymous classes. In particular, anonymous classes that are implementations of interfaces with a single method (such as Runnable you use twice) can be simplified in the form of lambda expressions .

So your code is with lambdas, arguably simpler:

public void onCreate(Bundle icicle) {
    ...
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mediaPlayer) {

            new Thread(() -> {
                while (!Thread.interrupted() && isActive) {
                    try {
                        Thread.sleep(5000);
                        runOnUiThread(() -> Log.v("INFOS: THREAD", "Valor atribuido"));
                    } catch (InterruptedException e) {
                        // ooops
                    }
                }
            }).start(); // the while thread will start in BG thread
        }
    });
}

You do not have to worry about this warning . You can safely ignore it if you want.

In fact this warning is just a hint of how you can simplify your code, but it is only valid for Java 8 or higher. Or put another way (as in the message), not is valid for Java 7 or lower.

And finally, the fact that you're running a thread with the loop has no relation to this warning . What causes it is only the use of anonymous classes that implement Runnable .

    
03.07.2015 / 01:50