public static void main(String[] args) {
new Thread(new PrintThreadName(), "1").start();
new Thread(new PrintThreadName(), "2").start();
new Thread(PrintThreadName::new, "3").start();
new Thread(PrintThreadName::new, "4").start();
}
PrintThreadName::new是lambda表达式
表示用PrintThreadName构造方法代替new Thread(new Runable(){run(){}})的run方法。
所以线程3和4没有输出内容,但是你在构造方法里面打印,就会有输出,
要想有你要的效果你可以
PrintThreadName objectc=new PrintThreadName();
new Thread(objectc::run, "4").start();
介绍一下这种表达式:
A::B B代表方法,如果B是静态或构造,A必须是类名。如果B是实例方法造,A必须是对象。
同时上面new Thread()参数需要的是实现接口的对象,接口只有一个方法,所以可以用lambda表达式,要保证这唯一的方法与替代的他的
方法参数一样。显然::这种只能用于无参。除了用::,还能用()->{ },()与方法的()一样,这里表示无参,有参这样(s)->{}
即:new Thread(()->{ }, "4").start();