因为如果能够更改它们,可能会导致很多问题,请考虑以下事项:
public void count(){ int x; new Thread(new Runnable() { public void run() { while(x < 100) { x++; try { Thread.sleep(1000); }catch(Exception e){} } } }).start(); // do some more pre... for(x = 0;x < 5;x++) for(int y = 0;y < 10;y++) System.out.println(myArrayElement[x][y]); }这是一个粗略的示例,但您可以看到可能发生许多无法解释的错误。这就是变量必须为最终变量的原因。这是解决上述问题的简单方法:
public void count(){ int x; final int w = x; new Thread(new Runnable() { public void run() { int z = w; while(z < 100) { z++; try { Thread.sleep(1000); }catch(Exception e){} } } }).start(); // do some more pre... for(x = 0;x < 5;x++) for(int y = 0;y < 10;y++) System.out.println(myArrayElement[x][y]); }如果您需要更完整的说明,则有点像同步。Java希望防止您从多个线程中引用一个对象。以下是有关同步的一些信息:
- http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html
希望这对您有所帮助!



