界面java.util.concurrent.ExecutorService
有多个submit
方法,
第二个接受Runnable
任务参数。但是,我看到了一些这样的代码,首先是Counter
类:
public class Counter {
private int count;
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
和Demo
类:
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();
}
}
我想知道在Demo
类中,提交方法的参数是counter:: increment
,我认为它不是Runnable
任务对象。那么,有什么神奇的事情发生了吗?