Код: Выделить всё
// Approach 1
Thread n = new Thread(new NumberGenerator(100));
// Approach 2
Thread n = new Thread(new NumberGenerator(100)::run);
Код: Выделить всё
public class NumberGenerator implements Runnable {
private int limit;
public NumberGenerator(int limit) {
this.limit = limit;
}
@Override
public void run() {
for(int i = 0; i < limit; i++) {
System.out.println(i);
}
}
}
public class Main {
public static void main(String[] args) {
// Which approach is better and why?
Thread t1 = new Thread(new NumberGenerator(100));
Thread t2 = new Thread(new NumberGenerator(100)::run);
t1.start();
t2.start();
}
}
Пока только Я знаю, что оба подхода используют этот конструктор в Thread.java
Код: Выделить всё
public Thread(Runnable target) {
this(null, target, "Thread-" + nextThreadNum(), 0);
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... hes-in-jav