我想在10個線程併發Java線程上從一個運行一個簡單的總和到上限功能。我目前的主要問題是放置upperBound,lowerBound,sum和i的變量。我想捕捉我變量更新總和計算,但我得到的錯誤「局部變量需要宣佈最終。」我是Java的初學者,但從我讀過的關於聲明變量final的內容來看,它是否使變量無法修改 - 不是我的目標......對吧?變量放置的Java線程問題
public class threads {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());
for(int i = 0; i < 10; i++) {
new Thread("" + i) {
public void run() {
int sum = 0;
int upperBound = 22;
int lowerBound = 1;
long threadID = Thread.currentThread().getId();
for (int number = lowerBound; number <= upperBound; number++){
sum = sum + number + i;
}
System.out.println("Thread: " + threadID + " is running now and will compute the sum from 1 to " + (upperBound + i));
System.out.println("Thread id " + threadID + " computes sum " + sum);
}
}.start();
}
}
}