编写10个线程,第一个线程从1加到10,第二个线程从11加到20…第十个线程从91加到100,最后再把十个线程结果相加。
class Addition extends Thread{
private int currNum;
private static int sum;
public Addition(int currNum){
this.currNum = currNum;
}
public static synchronized void add(int num){
sum = sum + num;
}
public void run(){
int sum = 0;
for(int i=0; i<10; i++){
sum = sum + currNum + i;
}
add(sum);
}
public int getSum(){
return sum;
}
}
public class text10{
public static void main(String []a){
Thread[] threadList = new Thread[10];
for(int i = 0;i<10;i++){
threadList[i] = new Addition(10*i + 1);
threadList[i].start();
}
Addition a1 = new Addition(0);
System.out.println("Sum is : "+ a1.getSum());
}
}