解法一:
* 10个线程
* */
//创建10个线程,线程一从1加到10,线程2从11加到20....最后把十个线程的结果相加。
/**
* 思路:
* 1.创建一个类,实现runnable接口,
* 2.定义2个成员变量,线程的初始值,线程的总量,
* 3.定义一个设置线程初始值的方法和获取线程总量的方法
* 4.重写run方法,每一个线程除了初始值不一样外,相加的类容都一样,获取单个线程的量。
* 5.将每个线程的量都相加。
* 6.在,main方法中创建共享数据对象
* 7.创建10个线程的的数组
* 8.遍历数组,获取每一个线程,给其赋初始值,
* 9.开启线程,并让线程插队
*
*/
public class 多线程_10个线程_100的和 {
public static void main(String[] args) throws InterruptedException {
//创建共享资源数据
MyThread4 my = new MyThread4();
//创建10个线程数组
Thread[] arr=new Thread[10];
//遍历该数组
for (int i = 0; i < arr.length; i++) {
//创建线程
Thread t=new Thread(my,"线程"+i);
//设置该线程的初始值
my.setStr(10*i+1);
//开启线程
t.start();
//线程等待
t.join();
}
System.out.println(MyThread4.getSum());
}
}
class MyThread4 implements Runnable{
private int str;//线程的初始值
private static int sum;//所有线程数量和
//设置线程赋初始值方法
public void setStr(int str){
this.str=str;
}
//获取所有线程的数量的方法
public static int getSum(){
return sum;
}
//重写run方法
public void run() {
//设置初始值
int num=0;
//获取一个线程的量
for (int i = 0; i < 10; i++) {
num+=str+i;
}
//调用添加方法
add(num);
}
//把每一个线程的量相加
private void add(int num) {
sum+=num;
}
}
解法二:
public class 线程100 {
private static int num=100;
static ArrayList<Integer> list = new ArrayList<>();
static int s1=0;
static int s2=0;
static int s3=0;
static int s4=0;
static int s5=0;
static int s6=0;
static int s7=0;
static int s8=0;
static int s9=0;
static int s10=0;
public static void main(String[] args) throws InterruptedException {
final Test01 t = new Test01();
System.out.println("--------------1----------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=90){
break;
}
for (int i = 1; i <=10; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s1=count/10;
}
}.start();
System.out.println("--------------2----------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=80){
break;
}
for (int i = 11; i <=20; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s2=count/10;
}
}.start();
System.out.println("--------------3----------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=70){
break;
}
for (int i = 21; i <=30; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s3=count/10;
}
}.start();
System.out.println("--------------4----------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=60){
break;
}
for (int i = 31; i <=40; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s4=count/10;
}
}.start();
System.out.println("--------------5----------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=50){
break;
}
for (int i = 41; i <=50; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s5=count/10;
}
}.start();
System.out.println("--------------6----------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=40){
break;
}
for (int i = 51; i <=60; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s6=count/10;
}
}.start();
System.out.println("-------------7-----------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=30){
break;
}
for (int i = 61; i <=70; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s7=count/10;
}
}.start();
System.out.println("--------------8----------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=20){
break;
}
for (int i = 71; i <=80; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s8=count/10;
}
}.start();
System.out.println("---------------9---------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=10){
break;
}
for (int i = 81; i <=90; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s9=count/10;
}
}.start();
System.out.println("--------------10----------");
new Thread(){
public void run(){
int count=0;
while(true){
synchronized(t){
if(num<=0){
break;
}
for (int i = 91; i <=100; i++) {
count+=i;
}
num--;
}
}
System.out.println(count/10);
list.add(count/10);
s10=count/10;
return;
}
}.start();
int c=0;
//目的是让前面的线程执行完了,之后才让这个线程睡觉,睡的时间不够长的话,结果会出现问题
Thread.currentThread().sleep(10);
for (int i = 0; i < list.size(); i++) {
c+=list.get(i);
}
System.out.println("List集合中打印的结果:"+c);
Thread.currentThread().sleep(10);//目的是让前面的线程执行完了,,睡的时间不够长的话,结果会出现问题
//System.out.println(list.size());
// System.out.println("总数为:"+c/10);
int k =s1+s2+s3+s4+s5+s6+s7+s8+s9+s10;
System.out.println("总数为:"+k);
}
}
解法三:
public class 线程求和 extends Thread{
static int num = 0;
int number;
public 线程求和(int n) {
number = n;
}
public static int getNum() {
return num;
}
@Override
public void run() {
int count = 0;
for (int i = 0; i < 10; i++) {
count += number + i;
}
num = count;
System.out.println(number + "+" + (number + 1) + "+...+" + (number + 9) + "=" + count);
}
public static void main(String[] args) throws InterruptedException {
int a = 0;
for (int i = 0; i < 10; i++) {
int k = i * 10 + 1;
线程求和 tt = new 线程求和(k);
tt.start();
tt.sleep(10);
a = a + tt.getNum();
}
System.out.println(a);
}
}
解法四:
public class Demo09_10个线程的和 {
@SuppressWarnings("static-access")
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
int k = i * 10 + 1;
ThreadTest tt = new ThreadTest(k);
tt.start();
}
Thread.currentThread().sleep(100);
System.out.println("-----"+ThreadTest.num);
}
}
class ThreadTest extends Thread {
int number;
static int num;
public ThreadTest(int n) {
number = n;
}
public void run() {
int count = 0;
for (int i = 0; i < 10; i++) {
count += number + i;
}
System.out.println(number + "+" + (number + 1) + "+...+" + (number + 9)
+ "=" + count);
num+=count;
}
}
|
|