type mismatch between java.util.concurrent.ExecutorService and java.util.concurrent.ScheduledExecutorService

108 views
Skip to first unread message

stone

unread,
Mar 5, 2012, 12:21:35 PM3/5/12
to scala-user
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

Alan Mimms

unread,
Mar 5, 2012, 12:33:28 PM3/5/12
to scala...@googlegroups.com
Don't you want to call Executors.newScheduledThreadPool(1) instead? Executors.newFixedThreadPool() returns ExecutorService which is what the compiler is complaining about. Executors.newScheduleThreadPool() returns ScheduledExecutorService which is what you want, no?

Cheers.

Dave

unread,
Mar 5, 2012, 12:35:12 PM3/5/12
to scala-user
You translate
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
to
val scheduler:ScheduledExecutorService =
Executors.newFixedThreadPool(1)
which is different

It should be:
val scheduler:ScheduledExecutorService =
Executors.newScheduledThreadPool(1)


On 5 mrt, 18:21, stone <stones....@gmail.com> wrote:
> I tried translate the java version athttp://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Schedul...
>
>  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/Schedul...

Dave

unread,
Mar 5, 2012, 12:43:30 PM3/5/12
to scala-user
Actually:
private final val scheduler:ScheduledExecutorService =
Executors.newScheduledThreadPool(1)

or if you want object privacy:

private[this] final val scheduler:ScheduledExecutorService =

stone

unread,
Mar 5, 2012, 10:12:19 PM3/5/12
to scala...@googlegroups.com
Thank you all for the suggestions. I stay up late last night, seems that my brain was not in debug mode :-)
Reply all
Reply to author
Forward
0 new messages