About Question enthuware.ocpjp.v8.2.1256 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
oscar_
Posts: 7
Joined: Sat Feb 27, 2016 5:56 am
Contact:

About Question enthuware.ocpjp.v8.2.1256 :

Post by oscar_ »

For the program to compile an import is needed:

Code: Select all

import java.util.concurrent.*;
For the program to execute it also needs a main method

Code: Select all

    public static void main(String args[]) {
        new BeeperControl().beepForAnHour();
    }
There is also a possiblity to improve the program by using lambdas:

Code: Select all

        final ScheduledFuture<?> beeperHandle =
        scheduler.scheduleAtFixedRate(() -> System.out.println("beep"), 10, 10, SECONDS);
        scheduler.schedule(() -> beeperHandle.cancel(true), 60 * 60, SECONDS);
So I suggest changing the example program to:

Code: Select all

import static java.util.concurrent.TimeUnit.*;
import java.util.concurrent.*;

class BeeperControl {
    private final ScheduledExecutorService scheduler =
        Executors.newScheduledThreadPool(1);
    
    public void beepForAnHour() {
        final ScheduledFuture<?> beeperHandle =
        scheduler.scheduleAtFixedRate(() -> System.out.println("beep"), 10, 10, SECONDS);
        scheduler.schedule(() -> beeperHandle.cancel(true), 60 * 60, SECONDS);
    }
    
    public static void main(String args[]) {
        new BeeperControl().beepForAnHour();
    }
}

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1256 :

Post by admin »

Updated.
thank you for your feedback!
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests