用Java的三个线程写10000以内的偶数和
展开全部
public class ThreeThread {
public static void main(String[] args) {
int threadCount = 3;// 定义线程数
int maxNumber = 10000;// 定义最大值
int sum = 0;//最后的一个结果
EvenThread[] threads = new EvenThread[threadCount];// 定义线程数组
for (int i = 0; i < threadCount; i++) {
int start = i * maxNumber / 3 + 1;
int end = (i + 1) * maxNumber / 3;
threads[i] = new EvenThread(start, end);//每个线程计算的范围
new Thread(threads[i]).start();//线程开始
}
// 用来判断是否每个线程都结束了,不另起线程,直接利用主线程
while (true) {
sum = 0;
boolean finished = true;//用来标识是否全部完成
for (EvenThread e : threads) {
finished = finished && e.finished;//只要有一个不完成,则finished为false
sum += e.getSum();
}
if (finished) {//如果全部完成,则不循环了
break;
}
}
System.out.println("和为:" + sum);//打印出和
}
}
//计算线程
class EvenThread implements Runnable {
private int start, end, sum;//定义范围及这个线程的和
public boolean finished;//标识当前线程是否已经完成
public EvenThread(int start, int end) {//构造方法
this.start = start;
this.end = end;
}
public int getSum() {//返回这个线程所计算出来的和
return this.sum;
}
public void run() {
System.out.printf("计算偶数和从%d到%d...\n", start, end);
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
sum += i;
}
}
finished = true;
System.out.printf("计算偶数和从%d到%d结束\n", start, end);
}
}
public static void main(String[] args) {
int threadCount = 3;// 定义线程数
int maxNumber = 10000;// 定义最大值
int sum = 0;//最后的一个结果
EvenThread[] threads = new EvenThread[threadCount];// 定义线程数组
for (int i = 0; i < threadCount; i++) {
int start = i * maxNumber / 3 + 1;
int end = (i + 1) * maxNumber / 3;
threads[i] = new EvenThread(start, end);//每个线程计算的范围
new Thread(threads[i]).start();//线程开始
}
// 用来判断是否每个线程都结束了,不另起线程,直接利用主线程
while (true) {
sum = 0;
boolean finished = true;//用来标识是否全部完成
for (EvenThread e : threads) {
finished = finished && e.finished;//只要有一个不完成,则finished为false
sum += e.getSum();
}
if (finished) {//如果全部完成,则不循环了
break;
}
}
System.out.println("和为:" + sum);//打印出和
}
}
//计算线程
class EvenThread implements Runnable {
private int start, end, sum;//定义范围及这个线程的和
public boolean finished;//标识当前线程是否已经完成
public EvenThread(int start, int end) {//构造方法
this.start = start;
this.end = end;
}
public int getSum() {//返回这个线程所计算出来的和
return this.sum;
}
public void run() {
System.out.printf("计算偶数和从%d到%d...\n", start, end);
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
sum += i;
}
}
finished = true;
System.out.printf("计算偶数和从%d到%d结束\n", start, end);
}
}
展开全部
这个有必要吗?写一个循环让i+=2,然后加到sum不就好了!用3个线程写的话你的意思是没个线程都要从0开始加到10000还是分成3部分写呢,从效率上来说没必要啊!java的线程不能同时运行的,只和计算的时间片和优先级有关系!不过java的优先级跨平台不是很好用!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先继承一个线程。run方法里面写上相关的运算式。然后引用三个对象进行debug
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
友友akfucc ,我刚刚测试了一下他的代码,好像有点问题!因为是线程,运行是不确定的,会出现混乱,所以我让程序里,停留了3秒,让程序运行完,但多次运行时会重复出现两个结果,一个是正确的,一个不知道是什么!但是当我在调试模式下,结果是正确的!我的代码是完全参考 友友akfucc 的代码写的,只在运行时出现了一点不确定性,修改了一下,希望友友akfucc 不要介意!
package test01;
public class EventThread implements Runnable {
private int start = 0;
private int end = 0;
private int sum = 0;
public boolean finish = false;
public EventThread(int start,int end) {
this.end = end;
this.start = start;
}
public void run() {
System.out.println("计算偶数和从%d到%d" +":"+start +"----"+ end);
for(int i = start; i <= end; i++) {
if(i % 2 == 0) {
sum += i;
}
}
finish = true;
System.out.println("从"+start+"到"+end +"的和为:"+sum);
}
public int getSum() {
return this.sum;
}
}
package test01;
public class ThreeThread {
//private int threadCount = 0;
private int start = 0;
private int end = 0;
private int maxNumber = 10000;
private int sum = 0;
private EventThread []thread = null;
public int getSum(int count) {
thread = new EventThread[count];
for(int i = 0; i < thread.length; i++) {
this.start = i * maxNumber / 3 + 1;
this.end = (i + 1) * maxNumber / 3;
thread[i] = new EventThread(start,end);
new Thread(thread[i]).start();
}
while(true) {
boolean finish = true;
//finish = finish & t.finish;
for(int i = 0;i < thread.length; i++) {
finish = finish & thread[i].finish;
sum += thread[i].getSum();
try {
Thread.sleep(2000); /*由于是线程,所以我们在次睡眠了3秒,让其完全运行完
但在多次运行时会出现一个正确的结果,一个不正确的,不知道为什么
望高手能修正,但在调试模式下,能得出正确结果*/
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
if(finish) {
break;
}
}
return sum;
}
}
package test01;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ThreeThread t = new ThreeThread();
// EventThread t = new EventThread();
int sum = t.getSum(3);
System.out.println(sum);
}
}
package test01;
public class EventThread implements Runnable {
private int start = 0;
private int end = 0;
private int sum = 0;
public boolean finish = false;
public EventThread(int start,int end) {
this.end = end;
this.start = start;
}
public void run() {
System.out.println("计算偶数和从%d到%d" +":"+start +"----"+ end);
for(int i = start; i <= end; i++) {
if(i % 2 == 0) {
sum += i;
}
}
finish = true;
System.out.println("从"+start+"到"+end +"的和为:"+sum);
}
public int getSum() {
return this.sum;
}
}
package test01;
public class ThreeThread {
//private int threadCount = 0;
private int start = 0;
private int end = 0;
private int maxNumber = 10000;
private int sum = 0;
private EventThread []thread = null;
public int getSum(int count) {
thread = new EventThread[count];
for(int i = 0; i < thread.length; i++) {
this.start = i * maxNumber / 3 + 1;
this.end = (i + 1) * maxNumber / 3;
thread[i] = new EventThread(start,end);
new Thread(thread[i]).start();
}
while(true) {
boolean finish = true;
//finish = finish & t.finish;
for(int i = 0;i < thread.length; i++) {
finish = finish & thread[i].finish;
sum += thread[i].getSum();
try {
Thread.sleep(2000); /*由于是线程,所以我们在次睡眠了3秒,让其完全运行完
但在多次运行时会出现一个正确的结果,一个不正确的,不知道为什么
望高手能修正,但在调试模式下,能得出正确结果*/
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
if(finish) {
break;
}
}
return sum;
}
}
package test01;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ThreeThread t = new ThreeThread();
// EventThread t = new EventThread();
int sum = t.getSum(3);
System.out.println(sum);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不懂你啥意思哦。说具体点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询