0

What does "new Loader()" in " Thread t = new Thread(new Loader()); " means from the below code?

class Loader implements Runnable { public void run() { System.out.println("Hello"); } } class MyClass { public static void main(String[ ] args) { Thread t = new Thread(new Loader()); t.start(); } } is " new Loader() " a new instance of Loader class without name?

7th Apr 2021, 6:44 AM
Mons Joseph
Mons Joseph - avatar
3 Answers
+ 2
Yes, just a shorter way of writing Loader load = new Loader(); Thread t = new Thread(load); t.start(); An even shorter way would be - new Thread(new Loader()).start();
7th Apr 2021, 7:21 AM
Soumik
Soumik - avatar
+ 1
zemiak I agree. Lambdas are pretty cool.
7th Apr 2021, 5:38 PM
Soumik
Soumik - avatar
0
or new Thread( ()-> System.out.println("Hello") ).start();
7th Apr 2021, 2:57 PM
zemiak