The interface java.util.concurrent.ExecutorService
has several submit
methods,
The second one accept a
Runnable
task parameter. However, I saw some code like this, first is the Counter
class:
public class Counter {
private int count;
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
and the Demo
class:
public class Demo {
private Counter counter = new Counter();
public void demoCounter() {
ExecutorService service = Executors.newCachedThreadPool();
IntStream.range(0, 1000)
forEach(i -> service.submit(counter:: increment));
service.shutdown();
System.out.println("Counter count=" + counter.getCount());
}
public void main(String[] args) {
Demo demo = new Demo();
demo.demoCounter();
}
}
What I wonder is that in the Demo
class, the submit method's parameter is counter:: increment
which I think is not a Runnable
task Object. So, was there something magic happened?