Topic: 大家来看一下关于2个1到50,51到100相加的线程题目

  Print this page

1.大家来看一下关于2个1到50,51到100相加的线程题目 Copy to clipboard
Posted by: 黑玫瑰之吻
Posted on: 2005-09-21 20:17

题目是这样的,写2个线程,一个线程是计算1到50的和,另一个是计算51到100的和的线程,要求调用并显示出每个线程运行时的数字,线程结束后并计算2个线程的和,我是这样作的:
class Shard
{
  static int sum=0;
  static int sum1=0;
  public synchronized static void set(int value,int value1)
  {
    sum=value;
    sum1=value1;
   System.out.println("2者之和"+(sum+sum1));
  
  }
  class Simple1 extends Thread
  {
   private Shard so=null;
public Simple1(Shard so){super("one");this.so=so;}
public void run()
{
  for(int i=1;i<=50;i++)
  {  
    sum+=i;
    System.out.println(getName()+sum);
    so.set(sum,sum1);
    try
      {
      sleep((int)(Math.random()*500));
      }
    catch(InterruptedException e){}
  }
    
}
  }
  class Simple2 extends Thread
  {
    private Shard so=null;
    public Simple2(Shard so){super("two");this.so=so;}
    public void run()
    {
      for(int i=51;i<=100;i++)
      {
        sum1+=i;
        System.out.println(getName()+sum1);
        so.set(sum, sum1);
        try
        {
       sleep((int)(Math.random()*500));
        }
        catch(InterruptedException e)
        {}
      }
    }
  }
}
public class Sum {

  public static void main(String[] args) {
    Shard as=new Shard();
    new Shard().new Simple1(as).start();
    new Shard().new Simple2(as).start();
    
  }
}

最后结果是5050,但是我这个程序,每运行一个线程时就计算出2个线程的结果,如何在线程结束后在把2个线程的和相加,并得出结果。
请各位帮帮忙,谢谢

2.Re:大家来看一下关于2个1到50,51到100相加的线程题目 [Re: 黑玫瑰之吻] Copy to clipboard
Posted by: jasonwijh
Posted on: 2005-09-23 16:10

我是个初学者,请大家多多指教。
下面是我写的代码:

class sum implements Runnable
{
  int star = 0;
  int end = 0;
  private int result = 0;
  public sum(int star, int end)
  {
    this.star = star;
    this.end = end;
  }
  
  public void run()
  {
    for(int i=star; i<=end; i++)
    {
      result+=i;
      System.out.println(Thread.currentThread().getName()+" is calculating from\t"
        +star+"\tto\t"+end+",\tnow is:"+i );  
    }
    
  }
  public int getresult()
  {
    return result;
  }
}

public class sum1
{
  public static void main(String[] args)
  {
    sum r1 = new sum(1 ,50 );
    sum r2 = new sum(51,100);
    Thread t1 = new Thread(r1,"t1");
    Thread t2 = new Thread(r2,"t2");
    t1.start();
    t2.start();
    try{
    t1.join();
    t2.join();
    }catch(InterruptedException e){
      System.out.println("the system is error!");
      e.printStackTrace();
    }
    System.out.println("the result after add is:"+(r1.getresult()+r2.getresult()));
  }
}

3.Re:大家来看一下关于2个1到50,51到100相加的线程题目 [Re: 黑玫瑰之吻] Copy to clipboard
Posted by: liner09136
Posted on: 2005-09-24 13:26

是不是把每个线程每次运行时的相加的结果显示一下比较好?
我也是来学习的。。。。。

package thread;

public class Summary {
  public static void main(String[] args){
    SumThread st1=new SumThread(1,50);
    SumThread st2=new SumThread(51,100);
    Thread t1=new Thread(st1,"线程1");
    Thread t2=new Thread(st2,"线程2");
    t1.start();
    t2.start();
    try{
     t1.join();
     t2.join();
     }catch(InterruptedException e){
     System.out.println("the system is error!");
     e.printStackTrace();
     }
     System.out.println("the result after add is:"+(st1.getSum()+st2.getSum()));
  }
}

class SumThread implements Runnable{
  int start;
  int end;
  int sum;
  public SumThread(int start,int end){
    this.start=start;
    this.end=end;
  }
  
  public void run(){
    for(int i=start;i<=end;i++){
      System.out.println(Thread.currentThread().getName()+" is calculate from "+start+" to "+end+" current result ="+(sum( i ) -sum(start-1)));
      if(i==end){
        System.out.println("此线程已结束!");
      }
    }
  }
  
  public int getSum(){
    return sum(end)-sum(start-1);
  }
  public int sum(int n){
  if(n<=0){
    return 0;
  }
  else
    return n+sum(n-1);
  }   
}

4.Re:大家来看一下关于2个1到50,51到100相加的线程题目 [Re: 黑玫瑰之吻] Copy to clipboard
Posted by: bwpc
Posted on: 2005-09-26 11:21

楼上的写的不错,但计算部分效率太低了
1. 重复计算, 每次打印都是重复计算的,当然最后取结果也是重新算一遍,呵呵

2. 递归调用效率太低了。

我觉得run方法以及其后的方法改成最最朴实的 一个一个累加 也许会好一些。

5.Re:大家来看一下关于2个1到50,51到100相加的线程题目 [Re: 黑玫瑰之吻] Copy to clipboard
Posted by: liner09136
Posted on: 2005-09-26 21:06

谢谢bwpc


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923