Page 1 of 1

About Question enthuware.ocpjp.v8.2.1256 :

Posted: Sat Feb 27, 2016 6:03 am
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();
    }
}

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

Posted: Sat Feb 27, 2016 10:21 am
by admin
Updated.
thank you for your feedback!
Paul.