Why the compiler alerts me Anonymous new Runnable() can be replaced with lambda

Asked

Viewed 127 times

4

Because that code (Thread) shows this message (on the compiler), and how to make it not show more?

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 the 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 Soen.

How can I write this code so that it stays correct? Thread executes a command each x seconds if isActive for true.

Attention to question tags.

1 answer

3


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

That’s your code with the Amblas, no doubt 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 don’t have to worry about that Warning. You can safely ignore it if you want.

Actually that 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 (such as in the message), nay is valid for Java 7 or lower.

And finally, the fact that you’re running a thread with the loop has nothing to do with this Warning. What causes it is only the use of anonymous classes that implement Runnable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.