timer - Lambda Schedule to println from a ArrayList -
i trying create timer print out arraylist @ schedule. receiving error:
cannot resolve method 'schedule(<lambda expression>,int,java.util.concurret.timeunit)'
why receiving error , can it?
list<string> finalquestionslist = questionslist; timer.schedule(()-> finalquestionslist.foreach(write::println),120, timeunit.seconds);
jls asserts type of lambda expression:
a lambda expression compatible in assignment context, invocation context, or casting context target type t if t functional interface type (§9.8) , expression congruent function type of ground target type derived t.
timertask not function interface, can't assign lambda expression timertask. can adaptee runnable timertask adapter method
task
.timer.schedule methods can't take
timeunit
, can using timeunit.tomills() convert duration milliseconds.i can't see
write
variable in code snippet, usesystem.out
instead.list<string> finalquestionslist = questionslist; timer.schedule(task(() -> finalquestionslist.foreach(system.out::println)) , 120, timeunit.seconds.tomillis(1)); private timertask task(runnable task) { return new timertask() { @override public void run() { task.run(); } }; }
Comments
Post a Comment