我不能在方法 run() 中使用变量“i”。有什么办法吗?
public class Main {
public static void main(String[] args) {
final int M = 100;
final int N = 4;
final int[] array = new int[M];
for(int b = 0; b < M; b++) array[b] = b;
for( int i = 0; i < N; i++) {
new Thread(new Runnable() {
public void run() {
for(int a = i*(M/N);a < (i+1)*(M/N); a++)
System.out.println("Thread "+i+":"+array[a]);
// i -> cannot refer to a non-final variable inside an inner class defined in a different method
}
}).start();
}
}
}