package day0611;
/**
* 创建20个线程,第一个从1加到5,第二个从6加到10,以此类推,加到100,最后把每个线程的结果相加
*
* @author acer
*
*/
public class Demo4 {
static int count;
static int allSum;
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(){
public void run() {
synchronized (Demo4.class) {
int sum=0;
for (int j = count*5+1; j <=(count+1)*5; j++) {
sum+=j;
}
System.out.println(sum);
count++;
allSum+=sum;
if (count==20) {
System.out.println(allSum);
}
}
}
}.start();
}
}
}
|