I tried translate the java version at
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
to the following scala code
ScheduledExecutorServiceDemo.scala
// run with `scala ScheduledExecutorServiceDemo.scala`
// ref :
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html
import java.util.concurrent.{Executor, Executors, ExecutorService,
ScheduledExecutorService, TimeUnit}
val thread = new Runnable {
def run() {
println("scheduled in thread...")
}
}
val scheduler:ScheduledExecutorService =
Executors.newFixedThreadPool(1)
scheduler.scheduleWithFixedDelay(thread, 5, 5, TimeUnit.SECONDS)
But the scala compiler complains about type mismatch:
scala ScheduledExecutorServiceDemo.scala
~/debug/ScheduledExecutorServiceDemo.scala:9: error: type mismatch;
found : java.util.concurrent.ExecutorService
required: java.util.concurrent.ScheduledExecutorService
val scheduler:ScheduledExecutorService =
Executors.newFixedThreadPool(1)
^
one error found
Any suggestions why the scala version don't work ?
Thanks,
Stone