package net.paoyun.day18.thread.zuoye;
/**
* 编写10个线程,第一个线程从1加到10,第二个线程从11加到20…第十个线程从91加到100, 最后再把10个线程结果相加。
*
* @author 11297
*
*/
public class ZuoYe4 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int k = i * 10 + 1;
Add add = new Add(k);
add.start();
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("10个线程结果相加为:" + Add.sum);
}
}
class Add extends Thread {
static int sum;
private int number;
public Add(int n) {
number = n;
}
@Override
public void run() {
int count = 0;
for (int i = 0; i < 10; i++) {
count = count + number + i;
}
System.out.println(number + "+" + (number + 1) + "+" + (number + 2) + "+" + (number + 3) + "+" + (number + 4)
+ "+" + (number + 5) + "+" + (number + 6) + "+" + (number + 7) + "+" + (number + 8) + "+" + (number + 9)
+ "=" + count);
sum = sum + count;
}
}
/**
* 编写10个线程,第一个线程从1加到10,第二个线程从11加到20…第十个线程从91加到100, 最后再把10个线程结果相加。
*
* @author 11297
*
*/
public class ZuoYe4 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int k = i * 10 + 1;
Add add = new Add(k);
add.start();
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("10个线程结果相加为:" + Add.sum);
}
}
class Add extends Thread {
static int sum;
private int number;
public Add(int n) {
number = n;
}
@Override
public void run() {
int count = 0;
for (int i = 0; i < 10; i++) {
count = count + number + i;
}
System.out.println(number + "+" + (number + 1) + "+" + (number + 2) + "+" + (number + 3) + "+" + (number + 4)
+ "+" + (number + 5) + "+" + (number + 6) + "+" + (number + 7) + "+" + (number + 8) + "+" + (number + 9)
+ "=" + count);
sum = sum + count;
}
}