jasonwijh
发贴: 2
积分: 0
|
于 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())); } }
|