Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:大家来看一下关于2个1到50,51到100相加的线程题目 [Re:黑玫瑰之吻]
jasonwijh





发贴: 2
积分: 0
于 2005-09-23 16:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我是个初学者,请大家多多指教。
下面是我写的代码:

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()));
  }
}




话题树型展开
人气 标题 作者 字数 发贴时间
4350 大家来看一下关于2个1到50,51到100相加的线程题目 黑玫瑰之吻 1404 2005-09-21 20:17
3399 Re:大家来看一下关于2个1到50,51到100相加的线程题目 jasonwijh 939 2005-09-23 16:10
3424 Re:大家来看一下关于2个1到50,51到100相加的线程题目 liner09136 1995 2005-09-24 13:26
3348 Re:大家来看一下关于2个1到50,51到100相加的线程题目 bwpc 116 2005-09-26 11:21
3543 Re:大家来看一下关于2个1到50,51到100相加的线程题目 liner09136 6 2005-09-26 21:06

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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