About Question enthuware.ocpjp.v8.2.1256 :
Posted: Sat Feb 27, 2016 6:03 am
For the program to compile an import is needed:
For the program to execute it also needs a main method
There is also a possiblity to improve the program by using lambdas:
So I suggest changing the example program to:
Code: Select all
import java.util.concurrent.*;
Code: Select all
public static void main(String args[]) {
new BeeperControl().beepForAnHour();
}
Code: Select all
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(() -> System.out.println("beep"), 10, 10, SECONDS);
scheduler.schedule(() -> beeperHandle.cancel(true), 60 * 60, SECONDS);
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();
}
}